01-学习iOS的动态添加Button和监听UIAlertView按钮的点击事件()

1、在h文件中定义,第一个按钮对应-(IBAction)  addButton:(id)sender; 并在m文件中实现它。

   -(IBAction)addButton:(id)sender{

    CGRect frame = CGRectMake(9020020060);

    UIButton *someAddButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    someAddButton.backgroundColor = [UIColor clearColor];

    [someAddButton setTitle:@"动态添加一个按钮!" forState:UIControlStateNormal];

    someAddButton.frame = frame;

    [someAddButton addTarget:self action:@selector(someButtonClicked)forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:someAddButton];

}

    这里还涉及到第二个方法someButtonClicked。

2、我们下面实现它

 

-(void) someButtonClicked{

 

     UIAlertView *alert = [[[UIAlertView allocinitWithTitle:@"提示"

                                                    message:@"您点击了动态按钮!"

                                                   delegate:self

                                          cancelButtonTitle:@"确定"

                                          otherButtonTitles:@"第二项"@"第三项"nilautorelease];

    [alert show];

}

以上两个方法实现对第一个按钮的监听,在第一个按钮被点击之后,动态生成第二个按钮;  如果在点击第二个按钮,则弹出提示信息框。

下面要实现对UIAlertView的监听。

3、首先要在h文件遵守协议<UIAlertViewDelegate>,具体代码是

 

@interface ViewController : UIViewController<UIAlertViewDelegate>

.........

@end

在m文件中实现alertView的若干方法,这里只实现一个方法

 

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{

    NSLog(@" button index=%d is clicked.....", buttonIndex);

}

猜你喜欢

转载自zhengjj-2009.iteye.com/blog/1943053