How to write ok button action in Objective-c?

I am new to ... I created a dialog and an ok button in the dialog. Now I am doing the dialogue operation. After that, I want to click ok button to close the dialog. for this i do as

in .h file

@interface viewcontroller:NSViewController

@property (weak) IBOutlet NSButton *OkBtn;

@end

      

in the .m file

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

+3


source to share


3 answers


Instead of creating an IBOutlet, you need to do IBAction

to directly receive the click event

Check out this image ...
enter image description here



This is the file .m

where I directly createIBAction

and if you want to give the clickable event programmatically then follow @Nicolas Buquet's answer

+3


source


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 method okButtonTapped:

will be called when you press the button and remove your finger (the "up" part).

+1


source


Create IBAction

for NSButton

. And connect IBAction

to the button.

in an interface file (.h) file,

- (IBAction) okButtonAction : (id) sender;

      

and in your implementation file (.m)

- (IBAction) okButtonAction : (id) sender {
   NSLog(@"OK Button action here");
}

      

0


source







All Articles