Trevor Rudolph
Trevor Rudolph

Reputation: 1063

Programing button in mac objective-c

i have some code here:

-(void)handleButton {
    NSLog(@"hello");
}

and i wanted to connect that function to occur when i press a button that is named, theButton I know that you can link things in the interface programer but that does not let me use functions, so how would i do this so i can use functions???

i've tried to use codes like this one:

[theButton setTarget:self];
[theButton setAction:@selector(handleButton:)];

to set the button to call that function but when i do it gives me an error that theButton is not declared, i know this is probably dumb and has a simple answer but i cant figure it out.

Upvotes: 0

Views: 172

Answers (1)

outis
outis

Reputation: 77400

The method takes no argument, so its selector would be specified as @selector(handleButton). However, this is incorrect. An action should take a single argument, the ID of the sender:

-(IBAction)handleButton:(id)sender {
    NSLog(@"hello");
}

As for interface builder, it does let you set functions as actions; that's the whole point. Without the IBAction, interface builder doesn't know that the method is supposed to be an action, so it won't present it as an option.

Both of these problems are good illustrations of why you should use code generators, as XCode would have generated the proper signature for you.

Upvotes: 1

Related Questions