如何将按钮链接到操作

时间:2013-07-03 02:39:23

标签: ios ibaction

我有使用C ++的经验,我需要知道如何使按钮转到我创建的void方法。

这基本上就是我的按钮

-(IBAction)Button:(id)sender {
}

如果没有办法,那么有没有办法将按钮链接到警报视图?

我将不胜感激。

3 个答案:

答案 0 :(得分:1)

您需要确保在-(IBAction)Button:(id)sender中定义@interface,然后在界面生成器中,右键单击+从Button对象拖动到文件所有者,然后选择 Button: (您的IBAction)

enter image description here

IBAction

中显示提醒
-(IBAction)Button:(id)sender
{
    UIAlertView *_alert = [[UIAlertView alloc] initWithTitle:@"Some title" message:@"Some message." delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK",nil];
    [_alert setTag:1];
    [_alert show];
    [_alert release];
}

答案 1 :(得分:1)

希望这会对你有所帮助。

UIButton *myButton=[[UIButton alloc] initWithFrame:CGRectMake(1.0f , 720.0f, 50.0f, 50.0f)];
[myButton addTarget:self action:@selector(myButtonTapped:) forControlEvents:UIControlEventTouchUpInside];

- (void)myButtonTapped:(id)iSender {
    //Code For Button Action
}

答案 2 :(得分:0)

试试这个

/* UIButton programmatically from a IBAction */
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(10,10,50,50);
[button addTarget:self action:@selector(buttonTouched:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];

-(IBAction)buttonTouched:(id)sender
{
    NSLog(@"New Button Clicked");
}