Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write the ok button action in Objective-c?

I am new in xcode. I have created a dialog and ok button on the dialog. Now, I am doing some operation on dialog. After that I want to click on ok button to close the dialog box. for this I am doing like

in .h file

@interface viewcontroller:NSViewController

@property (weak) IBOutlet NSButton *OkBtn;

@end

in .m file

"I don't know how to write the code for ok button in .m file. I just want to when click on OK button , just close the dialog.

like image 733
CrazyCoder Avatar asked May 13 '15 10:05

CrazyCoder


2 Answers

Instead of create IBOutlet, you have to make IBAction to directly get click event

check out this image...
enter image description here

This Is .m file where i create IBAction directly

and if you want to give click event programatically then follow @Nicolas Buquet answer

like image 200
EI Captain v2.0 Avatar answered Oct 30 '22 12:10

EI Captain v2.0


Add this to your code:

[OkBtn addTarget:self action:@selector(okButtonTapped:) forControlEvents:UIControlEventTouchUpInside];

and add this method to your class:

- (void)okButtonTapped:(UIButton *)sender {
NSLog(@"Ok button was tapped: dismiss the view controller.");
}

The okButtonTapped: method will be called when you tap iside the button and remove your finger (the 'up" part).

like image 31
Nicolas Buquet Avatar answered Oct 30 '22 10:10

Nicolas Buquet