简体   繁体   中英

NSButton doesn't respond

I'm now developing mac application for the first time. So here's my problem. At first time launch, the application shows login form inside a custom view in the main menu xib file, the login form is loaded from another NSViewController file. The problem is when I click the button at login form which is loaded in the main menu, it doesn't respond on click event. I've tried using performselector method, add Action , also using IBAction , none of them works.

Here's the code of appdelegate.m

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    // Insert code here to initialize your application
    NSViewController *loginformcontroller = [[LoginViewController alloc]initWithNibName:@"LoginViewController" bundle:nil];
    [window setContentView:loginformcontroller.view];
}

and at LoginViewController.m

@implementation LoginViewController @synthesize usernametxt,passwordtxt,loginbtn;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Initialization code here.
        [loginbtn setAction:@selector(checkalogin)];
    }
    return self;
}
-(void)checkalogin{
    NSLog(@"masuk");
}

Any suggestions?

在loginbtn setaction之后包括这一行: -

 [loginbtn setTarget:self];

You seem to be dynamically setting an action for a button loaded from a nib.

It is possible that you have not actually hooked up the button to an IBOutlet in the view controller, which means that you are setting a target action for a nil object.

A better way would be to hookup of the buttons action to the correct method from within Interface Builder itself.

I assume loginbtn has its equivalent in Interface Builder. If so, justctrl+drag from button to view controller to create an IBACTION. No need to add action in code.

I don't know if it is the same case as mine , but if you extend that NSButton and you override some of the methods like (void)mouseDown:(NSEvent *)theEvent; do not forget to call [super mouseDown:event]

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