简体   繁体   中英

Adding event handler to a cocoa button

I have a very simple Mac App built with XCode 4 (just a blank form for now). Now I've added a button via Interface Builder and a method to my AppDelegate:

-(IBAction) btnScanClicked
{
    NSLog(@"Hello!");
}

When I start my App I get the following message:

Could not connect the action btnScanClicked: to target of class AppDelegate

Does that mean I can't add any event handlers to my AppDelegate? Where else could I put them, and how would I set that up with Interface Builder?

The problem is that you haven't declared the action method properly. Replace the definition with:

-(IBAction) btnScanClicked:(id)sender {

Notice how the error message states that it couldn't connect to btnScanClicked: That colon at the end isn't punctuation - it's says that the message takes a parameter.

Unlike UIKit action methods need to be of the form:

- (void)methodName:(id)sender

IBAction just resolves to void , but is an indicator to Interface builder that it is an method that can be hooked up by context dragging.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM