views:

75

answers:

2

Hi,

My question may be a bit stupid, but why can't any object instantiated in IB handle, say, button click? I mean I'm able to add my own object to a xib, and link outlets to cotrols and control actions to object's method, but once I press the button everything just crashes (uknown selector).

Do you guys have a hint around that?

EDIT: The code, as requested:

@interface TextController {
    IBOutlet UILabel * textLabel;
    IBOutlet UITextField * textField;
}

-(IBAction)buttonClicked:(id)sender;

@end

@implementation TextController

-(IBAction)buttonClicked:(id)sender {
    textLabel.text = @"Ololo";
}

@end

Connections in IB are ok, just believe me. It's really hard to get them wrong with all this drag'n'drop stuff :)

EDIT 2: TextController is not a file owner (in this case it works fine). However, I just want to understand why I can't wire up an action to some object (may be even not a subclass of UIViewController).

A: 

post code, this usually means you dont have your connections wired up correctly. Is file's owner TextController in IB?

ennuikiller
Updated the post
Anton
No, he is not, and I don't want it to be a file owner. Why can't I just map some action to some arbitrary object?P.S. Mapping to file owner wroks fine, sure.
Anton
+1  A: 

You can wire outlets and actions to any object in the nib-file. Drag an NSObject form the library palette onto your nib-file, in Interface Builder. Then go to the Identity tab of the information palette and set the Class of your object.

This way you can instantiate any object of any class from your nib. If the target you want to hook to is statically created from the nib-file. Make sure that the file's owner have at least one reference to your object, or else it will be deallocated as soon as it has been created. Targets are not retained by the sender.

If the object you want to hook up should not be statically created from your nib, then implement awakeFromNib in a class that is instantiated from the nib-file and hook up the targets in code.

Last option is if you do not have any sub-class of your own in the nib-file at all. Then implement initWithNibName:bundle: in your UIViewController subclass, and hook up your targets in code after calling the super implementation.

PeyloW
Ah, man, you saved my brain! Thank you, really. I was about to give up programming when the whole image of how XIB works ruined in front of me. The problem was really about memory management. Crap, why I didn't know this?..
Anton