我正在创建iphone应用程序。按钮点击时如何触发事件?

时间:2010-08-20 21:52:54

标签: iphone objective-c cocoa-touch ipad

在xxx.h中

UIButton *b1, *b2, *b3;

在xxx.m

b1 = ---- similarly for b2 and b3

现在我希望在Click事件中将标题存储在字符串中。我怎么能实现它?

换句话说: 我必须在View Controller类中实现哪些函数/方法来处理UIButton上的click事件?

2 个答案:

答案 0 :(得分:2)

你可以在Interface Builder里面进行修饰IBAction 然后我假设你有一个文本的UITextField。

由于您不使用Interface Builder,您需要:

-(void)getStringFromText:(id)sender {
    NSString *input = sender.titleLabel;
}



//In some start up method.
[b1 addTarget: target action: @selector(getStringFromText:) forControlEvents: UIControlEventTouchUpInside];

答案 1 :(得分:0)

当您声明UIButtons时,需要在类定义中放置“IBOutlet” - 然后您需要使用Interface Builder将UIButtons链接/引用到您在代码中声明的UIButtons。

完成此操作后,您需要开发一个(IBAction)方法 - 并让它只是从调用对象获取“text”属性。在Interface Builder中,为“修饰”事件的所有按钮引用此方法。

如果不清楚我会发布代码作为例子吗?