I'm new to mac development. I developed an application using the NSTabView in which al worked fine for me.
But later I wanted to change the UI so I used NSSplitView in this I'm able to display the xib, but the button action are not happening, and I'm getting an error:
Could not connect the action cancelAction: to target of class NSViewController".
- (IBAction)cancelAction:(id)sender
{
[self enableNewProductAction];
}
How can I fix this problem?
Somewhere, you'll have an object in your XIB file that has its class set to NSTableView instead of your custom subclass. Find that and change it, and you'll be set.
Related
I have simple non document, cocoa app in which I have added new NSWindowController, named Login and I created a .xib file for it. I want this window to be opened first when the app is started.
I have changed the main interface section to my project from MainMenu to Login, and when I run the app, indeed the new login window is started first.
However...none of the outlets work. I get error message for each outlet in my login window that looks like this:
Failed to connect (userNameTextField) outlet from (NSApplication) to (NSTextField): missing setter or instance variable
What I am doing wrong?
I encountered the same error message. The solution in my case may not be directly applicable but perhaps it will help in some way.
I created a TestWindow view controller with associated xib and set up some referencing outlets. I then programmatically created the window with NSWindowController* window = [[NSWindowController alloc] initWithWindowNibName:#"TestWindow"]; and got the same sort of error message. I finally realized that I should write [TestWindow alloc] instead of [NSWindowController alloc] and that fixed everything. It seems so obvious in retrospect but I'm still getting used to what Interface Builder does and doesn't do for you auto-magically.
Your error message says it can't connect from (NSApplication) instead of (YourAppClassName), so I suspect the class that you define your outlets in isn't actually being instantiated.
I've been fiddling with Objective-C + Cocoa and writing a fairly simple Cocoa application. Then I encountered a runtime error that has no effect on my program execution:
Could not connect the action textField: to target of class BarController
I'm experimenting with pulling some of the windows out of MainMenu.xib and loading them with separate controllers and xib files.
In one window within a new xib I've created a Text Field and linked it to an IBOutlet NSTextField (textField) in a new NSWindowController subclass. It works and I am able to use textField to update the contents of the Text Field.
I am curious why I am getting the above runtime error and I'm hoping that understanding it will clear up some of the magic around the UI construction process.
Not much magic with plain IBOutlets, really - the fun starts when using Bindings or actions sent to some yet unknown target via First Responder and the responder chain :-)
Regarding your error:
Sounds like you've connected an outlet to a target that doesn't exist in your target class?
You might have established a connection to a property in File Owner (or some other proxy object) at some point where the owner's class was e.g. MyAppDelegate.
Then you've moved the window (or other object containing the outlet) to some other .xib and now the owner's class is MyWindowController that doesn't have a property of the same name you've connected your outlet to - and voila, the runtime won't be able to establish the connection for your outlet at runtime.
Just double-check your outlets in the Interface Editor, there's probably already some kind of warning message displayed next to outlets that look fishy.
Delete or reassign them and you're done.
I come from a Cocoa touch background and there's one point I'm stuck on, I'm trying to create a ViewController from scratch and connect objects in my xib to actions in my new ViewController. I changed the file's owner to my ViewController and added outlets, and the compiler seems to be happy with what I've done.
But when I run my application, the connections seem to fail with: 'Could not connect the action start: to target of class NSApplication,' but where is this NSApplication reference hiding? To the best of my knowledge my ViewController has a class of NSViewController.
ViewController : NSViewController
I changed the file's owner to my ViewController and added outlets, and the compiler seems to be happy with what I've done.
Sure, the compiler won't care -- this is a run-time error. It sounds like you're working in your MainMenu.xib file. That file is loaded by the application object. Even though you changed the type of File's Owner in the .xib, the object that's loading the .xib is still the application.
You'll probably want to create a separate .xib file for your view controller to load. You change the type of File's Owner in that file to you view controller class! hook things up to File's Owner, and it should work fine since your view controller really will be the object that's loading the file.
I've started a new Navigation-based project in X-Code. I delete the default grid view, add a new blank view, and connect it to the File's Owner via ctrl+click+drag, and it builds fine, but before anything happens on the iPhone simulator or the real thing, I get "Thread 1: Program recieved signal: "SIGABRT"" when the program hits "[self.window makeKeyAndVisible]". I'm really new to Objective-C and X-Code, but I know quite a bit of C# and C++. I don't really understand if I've set something in the IDE wrong, or what...any help would be greatly appreciated. Thanks!
When you create a new navigation project, the RootViewController class is a subclass of UITableViewController. Your error is due to removing the UITableView from the xib file, and replacing it with a UIView.
To fix this, you should change the super class of RootViewController to UIViewController. (Don't forget to remove the UITableView datasource and delegate methods from the implementation file for clarity.)
I want to incorporate an applicationDidFinishLaunching: into my cocoa delegate. How would I do this?? On the iphone SDK the applicationDidFinishLaunching is already in the application delegate, but when making my mac application I noticed that there were none.
Best Regards,
Kevin
As of Xcode 3.2, the Mac application template also comes with an application delegate, already connected, that has such a method.
To set this up in a project created before Xcode 3.2, create a new class for your delegate to be an instance of. I usually name mine “AppDelegate”. You'll do this by right-clicking on the Classes group and choosing “Add File”, then picking the Cocoa NSObject Subclass file template.
Open the header you just created (AppDelegate.h). Give it any instance variables you want. Then hit Go to Counterpart. That takes you to the implementation file (AppDelegate.m). Add your applicationDidFinishLaunching: instance method here. Unlike on the iPhone, this is a notification-handler method, so it takes an NSNotification instance and not an NSApplication instance.
Now to hook it up. In the Resources group, open MainMenu.nib. Drag an Object from the Library window into the top-level nib window (the one with icons in it, such as File's Owner and First Responder). Select the object you just created and open the Identity inspector. Set the object's class to AppDelegate, matching the name you used in Xcode. Right-click on the File's Owner, and drag from its delegate outlet to your new object.
In Xcode, add an NSLog statement to your applicationDidFinishLaunching: method. Hit Save All, then Build and Go. Switch back to Xcode and open the Debugger Console. If you did everything right and I didn't forget anything, you should see the log message there.
- (id)init
{
if (self = super init]) {
[NSApp setDelegate:self];
}
return self;
}
You can also do this in Interface Builder; from "File's Owner" in MainMenu.xib, just drag the "delegate" outlet to your object. You may want to consider using -awakeFromNib instead though.
Were you missing the application delegate files altogether? It seems as though there's a bug in the Xcode installation scripts (at least for 3.2.1 on Snow Leopard) that installs the latest project templates in the wrong folder. The older template for a "Cocoa Application" project doesn't contain the delegate files.
I've explained what I've discovered (and how I "fixed" it) in a blog post called Fixing the Xcode Project Templates.
Cheers,
Graham