Pg. 90: Setting the Targets


#1

For the first example for drawMickey:, where would you place the code? In the init method?

SEL mySelector;
mySelector = @selector(drawMickey:);
[myButton setAction:mySelector];

If you set the target programmatically, do you have to leave the instantiated objects in the NIB unconnected?


#2

Ok, answered my own question. It will not work in the init method. You have to put it into the awakeFromNib: method. Furthermore, for the SpeakLine example, you also have to set the target even though I declared the pointers for the buttons with IBOutlet in the header file and connected the buttons in the NIB.

- (void) awakeFromNib {

	// another way to connect to the speakButton and stopButton programatically
	SEL mySelector;

	mySelector = @selector(sayIt:);
	[speakButton setTarget:self];
	[speakButton setAction:mySelector];
	
	mySelector = @selector(stopIT:);
	[stopButton  setTarget:self];
	[stopButton  setAction:mySelector];
}

Connecting the action in the NIB is a lot easier.


#3

[quote=“steffenfrost”]Furthermore, for the SpeakLine example, you also have to set the target even though I declared the pointers for the buttons with IBOutlet in the header file and connected the buttons in the NIB.
[/quote]
Ummm…what exactly was connected in the NIB?

It sounds like you connected from the APP CONTROLLER to the BUTTON. This would given you a reference to the button (that is, instantiate the IBOutlets you declared for the buttons) so you could do things like change the label of a button. However, that would NOT do anything to affect what happens when you press the button.

The NIB-equivalent of the code would be control-dragging from the BUTTON to the APP CONTROLLER and selecting sayIt: or stopIT:.


#4

Post deleted, bad info given.