xcode 如何在 ViewController 中为 UIButton 创建事件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34252464/
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
How to create event for UIButton in ViewController?
提问by Alan Hoo
I add a UIButton
programmatically, and I want to call a method when pressing on it, I'm not sure how to design such a pattern:
我以UIButton
编程方式添加了一个,我想在按下它时调用一个方法,我不知道如何设计这样的模式:
where should I put event handler, or action method? in view controller or view itself.
If I put the method in view controller, how can I manipulate the
SubViews
of the View in the method? should I expose all ofSubViews
(UIButton
, etc.) to controller by putting them in view's header file as properties? Actually this question shall be asked in this way: How can I implement by code thatSubViews
in a view are associated to anIBOutlet
properties by Interface Builder...
我应该把事件处理程序或操作方法放在哪里?在视图控制器或视图本身中。
如果我将方法放在视图控制器中,我如何操作方法中的视图
SubViews
?我应该通过将它们作为属性放在视图的头文件中来将所有SubViews
(UIButton
等)公开给控制器吗?实际上这个问题应该以这种方式提出:我如何通过SubViews
在视图中IBOutlet
通过接口生成器关联到属性的代码来实现...
回答by Roy K
Check this link for the basics of iOS view hierarchy: Getting started with iOS Viewsand understand the following diagram (credits: techrepublic.com):
检查此链接以了解 iOS 视图层次结构的基础知识:iOS 视图 入门并了解下图(来源:techrepublic.com):
Programmatically:
以编程方式:
// Create your button wherever you wish (below is example button)
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
myButton.frame = CGRectMake(0.0, 0.0, 320, 450);
[myButton setTitle:@"Yay" forState:UIControlStateNormal];
[myButton addTarget:self action:@selector(didTouchUp:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:myButton];
// This method will be called when touch up is made on the button
- (void)didTouchUp:(UIButton *)sender {
NSLog(@"Button Pressed!");
}
Explanation:
解释:
[myButton addTarget:self action:@selector(didTouchUp:) forControlEvents:UIControlEventTouchUpInside];
- myButton- your button view
- addTarget- the target in which the method exist in
- action- the selector you want to call
- forControlEvents- control event the user will do to trigger this action
- myButton- 你的按钮视图
- addTarget- 方法所在的目标
- action- 要调用的选择器
- forControlEvents- 用户将执行的控制事件以触发此操作
Using Storyboards:
使用故事板:
- (A) Make sure the storyboard is corresponding to the correct class
- (B) Drag UIButton from the object library into the storyboard
- (A) 确保故事板对应正确的类
- (B) 将对象库中的 UIButton 拖入故事板
Then add to the ViewController.h file:
然后添加到 ViewController.h 文件中:
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@property (weak, nonatomic) IBOutlet UIButton *myButton;
- (IBAction)didTouchUp:(id)sender;
@end
Then add to the ViewController.m file:
然后添加到 ViewController.m 文件中:
- (IBAction)didTouchUp:(id)sender {
NSLog(@"Button Pressed!");
}
Lastly, create the connection between the UIButton and the IBAction:
最后,创建 UIButton 和 IBAction 之间的连接:
- (A) Right click on the button view
- (B) Drag touch up action to the button view on the storyboard
- (C) Press on didTouchUp:
- (A) 右键单击按钮视图
- (B) 将 touch up action 拖到 Storyboard 上的按钮视图
- (C) 按下 didTouchUp:
This is the very basic flow to do such a step... you might want to expend your skills by reading the following:
这是执行此步骤的非常基本的流程……您可能希望通过阅读以下内容来扩展您的技能: