xcode 如何在Objective-c中编写ok按钮动作?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30211495/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 07:08:20  来源:igfitidea点击:

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

objective-cxcodemacos

提问by CrazyCoder

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

我是xcode新手。我在对话框上创建了一个对话框和确定按钮。现在,我正在对对话框进行一些操作。之后我想单击确定按钮关闭对话框。为此,我正在做

in .h file

在 .h 文件中

@interface viewcontroller:NSViewController

@property (weak) IBOutlet NSButton *OkBtn;

@end

in .m file

在 .m 文件中

"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.

“我不知道如何在 .m 文件中编写 ok 按钮的代码。我只想在单击 OK 按钮时关闭对话框。

回答by EI Captain v2.0

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

而不是创建IBOutlet,你必须IBAction直接获取点击事件

check out this image...
enter image description here

看看这张图片...
在此处输入图片说明

This Is .mfile where i create IBActiondirectly

这是.mIBAction直接创建的文件

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

如果您想以编程方式提供点击事件,请按照@Nicolas Buquet 回答

回答by Nicolas Buquet

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).

okButtonTapped:当您点击按钮旁边并移开手指(“向上”部分)时,将调用该方法。

回答by Abdul Yasin

CreateIBActionforNSButton. And connect IBActionto the button.

创建IBActionNSButton。并连接IBAction到按钮。

in interface file (.h) file write,

在接口文件 (.h) 文件中写入,

- (IBAction) okButtonAction : (id) sender;

and in your implementation file (.m)

并在您的实现文件 (.m) 中

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