View Switching: Unrecognised selector sent to instance - macos

I made some example that changes view by clicking button.
When clicking the "fist" button in view, I keep getting the error:
[__NSArrayM changeLogView:]: unrecognized selector sent to instance 0x100539e30
Here is the simplified code:
DSWAppDelegate.h
#import <Cocoa/Cocoa.h>
#class DSWHomeViewController;
#interface DSWAppDelegate : NSObject <NSApplicationDelegate>
#property (assign) IBOutlet NSWindow *window;
#property (weak) IBOutlet NSButton *logBtn;
#property (weak) IBOutlet NSTabView *tabView;
#end
DSWAppDelegate.m
#import "DSWAppDelegate.h"
#import "DSWLogViewController.h"
#implementation DSWAppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
// Insert code here to initialize your application
[self.tabView setTabViewType:NSNoTabsNoBorder];
NSTabViewItem *item = nil;
DSWLogViewController *logvc = [[DSWLogViewController alloc initWithNibName:#"DSWLogViewController" bundle:[NSBundle mainBundle]];
item = [[NSTabViewItem alloc] initWithIdentifier:#"log"];
[item setView:logvc.view];
[self.tabView insertTabViewItem:item atIndex:0];
}
#end
DSWLogViewController.h
#import <Cocoa/Cocoa.h>
#interface DSWLogViewController : NSViewController
#property (weak) IBOutlet NSButton *firstBtn;
#property (weak) IBOutlet NSButton *secondBtn;
#property (weak) IBOutlet NSBox *box;
- (IBAction)changeLogView:(id)sender;
- (IBAction)testSelector:(id)sender;
#end
DSWLogViewController.m
#import "DSWLogViewController.h"
#implementation DSWLogViewController
- (IBAction)changeLogView:(id)sender
{
NSLog(#"changeLogView : %#", sender);
}
- (IBAction)testSelector:(id)sender
{
NSLog(#"testSelector : %#", sender);
}
#end
And I checked class name of file owner.

Something is sending a changeLogView: message to a mutable array instead of your view controller. Chances are, whatever you've connected the IBAction to is pointing to the wrong object - an array of some sort, rather than your view controller.

Related

Cocoa NSTextField

i have a problem with accessing value from the NSTExtField in different class here is the code:
AppDelegate.h
#import <Cocoa/Cocoa.h>
#interface AppDelegate : NSObject <NSApplicationDelegate>
#property (assign) IBOutlet NSWindow *window;
#property (weak) IBOutlet NSTextField *numberOfPhrases;
#end
AppDelegate.m
#import "AppDelegate.h"
#implementation AppDelegate
#synthesize numberOfPhrases;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
NSLog(#"%#",[numberOfPhrases stringValue]);
}
TestClass.h
#interface TestClass : NSObject
- (IBAction)doSomething:(id)sender;
#end
TestClass.m
#implementation TestClass
- (IBAction)doSomething:(id)sender {
NSLog(#"%#",[numberOfPhrases stringValue]); ?????????
}
You obviously can't access the text field value in another class without a link to it.
To access the text field's value you either need to have one more IBOutlet to it in this class or an IBOutlet to AppDelegate so that you can access its property.
TestClass.h
#interface TestClass : NSObject
{
IBOutlet NSTextField *numberOfPhrases; // connect it to the new referencing outlet of text field by dragging a NSObject object in your xib and setting its class to "TestClass"
}
- (IBAction)doSomething:(id)sender;
#end
OR an another option is to have a IBOutlet of AppDelegate in TestClass (because if you only create a new instance of AppDelegate and not its IBOutlet then a different instance of text field will be created and you will not be able to access the value of your text field)
TestClass.h
#interface TestClass : NSObject
{
IBOutlet AppDelegate *appDel; // connect in the xib
}
- (IBAction)doSomething:(id)sender;
#end
TestClass.m
#implementation TestClass : NSObject
- (IBAction)doSomething:(id)sender
{
[[appDel numberOfPhrases]stringValue]; //get the string value in text field
}
#end
The only thing you're missing is the addition to your TestClass.m file:
#import "TestClass.h"
#import "AppDelegate.h"
#implementation TestClass
- (IBAction)doSomething:(id)sender {
AppDelegate *theInstance = [[AppDelegate alloc] init];
[theInstance numberOfPhrases];
}
#end
You need to include the class header of AppDelegate.h in TestClass.m, then you simply call an instance through [[AppDelegate alloc] init]; You'll need to link your NSTextField to the Sent Actions in Interface Builder do:Something -> TestClass and Referencing Outlets numberOfPhrases -> AppDelegate.
Output:
2014-01-21 23:32:56.499 test[6236:303] Wonders Never Cease

When I call this method, it returns 0

Logic is: read data from a view (3 textfields) then when a button is pressed, sum data which stored in the model and print it out.
<--Header file-->
#import <Cocoa/Cocoa.h>
#class QuoteAttributes;
#interface AppDelegate : NSObject <NSApplicationDelegate>
#property (assign) IBOutlet NSWindow *window;
#property (weak) IBOutlet NSTextField *amountOfHardware;
#property (weak) IBOutlet NSTextField *amountOfPrinter;
#property (weak) IBOutlet NSTextField *amountOfSoftware;
#property (strong) QuoteAttributes *quickQuote;
- (IBAction)calculate:(id)sender;
- (void) setValueTotheForm;
#end
See the following Code:
#import "AppDelegate.h"
#import "QuoteAttributes.h"
#implementation AppDelegate
#synthesize amountOfHardware;
#synthesize amountOfPrinter;
#synthesize amountOfSoftware;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
// Insert code here to initialize your application
}
- (void) setValueTotheForm{
[self.quickQuote setNumHardware:[self.amountOfSoftware intValue]];
[self.quickQuote setNumPrinter:[self.amountOfPrinter intValue]];
[self.quickQuote setNumSoftware:[self.amountOfSoftware intValue]];
int totalQuote = [self.quickQuote numHardware] + [self.quickQuote numPrinter] + [self.quickQuote numSoftware];
NSLog(#"%d", totalQuote);
}
- (IBAction)calculate:(id)sender {
[self setValueTotheForm];
}
#end
However, when I call this method, it prints 0
should initiate the object first :
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
// Insert code here to initialize your application
QuoteAttributes *qa = [[QuoteAttributes alloc] init];
[self setQuickQuote: qa];
}
then the abovementioned code will work.

Arc Error ?? Existing ivar delegate for unsafe_unretained property

I am currently following the tutorials in a book that was written before the use of arc in Xcode. I have the exact same problem like what is stated here:
'Existing ivar 'delegate' for unsafe_unretained property 'delegate' must be __unsafe_unretained
The question has been answered, but is has not been stated exactly why this is the case.
The Code in the book states that I use this code to declare member variables:
the.h File
___________
#import <Cocoa/Cocoa.h>
#interface TestProgramDelegate : NSObject <NSApplicationDelegate> {
NSWindow *window;
NSTextField *message;
}
#property (assign) IBOutlet NSWindow *window;
#property (assign) IBOutlet NSTextField *message;
#property (assign) IBOutlet NSTextField *inputText;
#end
the.m file
__________
#import "the.h" //File
#implementation theHFileAppDelegate
#synthesize window
#synthesize message
#synthesize inputText
When I synthesize the variables in my .m file, I get the following error message:
Existing ivar 'window' for property 'window' with assign attribute must be __unsafe_unretaied.
But when I remove the declaration of the member variables like its stated in the book, everything works fine and I don't get an error.
the.h File
___________
#import <Cocoa/Cocoa.h>
#interface TestProgramDelegate : NSObject <NSApplicationDelegate> {
NSWindow *window;
NSTextField *message;
}
#property (assign) IBOutlet NSWindow *window;
#property (assign) IBOutlet NSTextField *message;
#property (assign) IBOutlet NSTextField *inputText;
#end
the.m file
__________
#import "the.h" //File
#implementation theHFileAppDelegate
#synthesize window
#synthesize message
#synthesize inputText
My program works, but I want to understand why this causes trouble. Thanks a lot.

No declaration of property ... found in interface - But there is

I have the following error: No declaration of "window" found in interface.
Though when I look there is one... There's probably something stupid I missed, but can't find it.
PlanetoidsAppDelegate.h
#import <Cocoa/Cocoa.h>
#import <WebKit/WebKit.h>
#interface WebViewExampleAppDelegate : NSObject {
NSWindow *window;
IBOutlet WebView *webView;
}
#property (assign) IBOutlet NSWindow* window;
#property (nonatomic, retain) IBOutlet WebView* webView;
#end
PlanetoidsAppDelegate.m
#import "PlanetoidsAppDelegate.h"
#implementation PlanetoidsAppDelegate
#synthesize window; //<-- error here
#synthesize webView; //<-- error here (well, the same one for webView that is...)
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
}
- (void)awakeFromNib {
NSString *resourcesPath = [[NSBundle mainBundle] resourcePath];
NSString *htmlPath = [resourcesPath stringByAppendingString:#"/Planetoids/Planetoids_Game.html"];
[[webView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:htmlPath]]]; //<-- error: webView undeclared, what makes sense considdering the other errors :P
}
#end
Can anyone here see the error?
Your interface is WebViewExampleAppDelegate but your implementation is PlanetoidsAppDelegate. These must match.
#interface WebViewExampleAppDelegate : NSObject in .h
#implementation PlanetoidsAppDelegate in .m
Two completely different classes.You need to be implementing WebViewExampleAppDelegate.m and synthesizing those methods in that class.
Also, for this:
#interface WebViewExampleAppDelegate : NSObject {
NSWindow *window;
IBOutlet WebView *webView;
}
try
#interface WebViewExampleAppDelegate : NSObject {
UIWindow *window;
IBOutlet WebView *webView;
}

Xcode Storyboard - Transfer of data

I'm new to storyboarding so the answer to this may be simplistic,
I've created a viewController in which a UITextField is present. My test is to transfer the data (text) from that text field into a viewController that is pushed onto the screen.
The coding that i have is as follows:
ViewController1.h -
#import <UIKit/UIKit.h>
#import "ViewController2.h"
#interface ViewController1 : UIViewController
#property (nonatomic, retain) IBOutlet UITextField *inputText;
#end
ViewController1.m
#import "ViewController1.h"
#implementation ViewController
#synthesize inputText;
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:#"showTextController"]) {
ViewController2 *vc2 = [segue destinationViewController];
vc2.selectedText.text = self.inputText.text;
}
}
#end
ViewController2.h -
#import <UIKit/UIKit.h>
#interface ViewController2 : UIViewController
#property (nonatomic, retain) IBOutlet UILabel *selectedText;
#end
ViewController2.m
#import "ViewController2.h"
#implementation ViewController2
#synthesize selectedText;
#end
The segue between viewController1 and 2 in storyboard is referred to as 'showTextController'.
Is this the correct coding for something so simple? Do i need to be using 'ViewDidLoad' method along with the prepareForSegue:sender method?
It looks correct to me. In fact, this is simpler than we have had in the past since the storyboard takes care instantiating our view controller objects. One thing to note is that if you're using ARC then you shouldn't be retaining your UILabel.
Hope this helps.

Resources