0

我是 iphone 新手..我创建了一个按钮并通过编码并想通过该按钮调用一个方法我该怎么办请帮助我..

UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
myButton.frame = CGRectMake(20, 20, 150, 44); // position in the parent view and set the size of the button
[myButton setTitle:@"Click Me!" forState:UIControlStateNormal];
// add targets and actions
[myButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
// add to a view
[myview addSubview:myButton];
4

5 回答 5

2

The answer is in the question itself

// add targets and actions
[myButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];

this line added a action to the button which is in self with action name buttonClicked: for the controlevent touchUpInside

so when a button is touchupInside it will execute the method buttonClicked

Therefore

- (void)buttonClicked: (UIButton *)sender
{
 // do whatever you want to do here on button click event
}

will get executed on button action

于 2013-06-10T08:38:05.093 回答
1
- (void)buttonClicked:(id)sender
{
   // your code
}
于 2013-06-10T08:13:34.900 回答
0
- (void)buttonClicked: (UIButton *)sender
{
 // do stuff
}
于 2013-06-10T08:14:19.510 回答
0
- (void)buttonClicked: (UIButton *)sender
{
 // do whatever you want to do here on button click event
}
于 2013-06-10T16:50:29.540 回答
0

看看UIControl 文档

- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents  

行动 : A selector identifying an action message. It cannot be NULL.

你是@selector(buttonClicked:)作为行动传递的。@selector()是一个编译器指令,用于将括号内的任何内容转换为SEL. ASEL是一种类型,表示方法名称,但不表示方法实现。[1]所以-(void)buttonClicked:(id)sender在你的课堂上实施。

于 2013-06-10T08:22:21.633 回答