歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> iOS開發教程之動態添加Button和監聽UIAlertView按鈕

iOS開發教程之動態添加Button和監聽UIAlertView按鈕

日期:2017/3/1 10:14:54   编辑:Linux編程

一、動態添加Button

動態添加Button的效果就是點擊之後,生成一個按鈕,並為按鈕添加點擊的方法。

1、在xib文件上拖拽添加一個button,標題為:添加button。

2、按住ctrl鍵拖拽到addbuttonViewController.m文件空白處,生成IBAction,填充代碼後如下:

  1. - (IBAction)addButton:(id)sender {
  2. CGRect frame = CGRectMake(90, 200, 200, 60);
  3. UIButton *someAddButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
  4. someAddButton.backgroundColor = [UIColor clearColor];
  5. [someAddButton setTitle:@"動態添加一個按鈕!" forState:UIControlStateNormal];
  6. someAddButton.frame = frame;
  7. [someAddButton addTarget:self action:@selector(someButtonClicked) forControlEvents:UIControlEventTouchUpInside];
  8. [self.view addSubview:someAddButton];
  9. }

3、動態生成的button點擊事件方法:

生成的button點擊彈出提示框。

  1. -(void) someButtonClicked{
  2. UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"
  3. message:@"您點擊了動態按鈕!"
  4. delegate:self
  5. cancelButtonTitle:@"確定"
  6. otherButtonTitles:nil];
  7. [alert show];
  8. }

4、編譯運行效果 圖1 2 3:

圖1:

點擊按鈕後

圖2:

圖3:

Copyright © Linux教程網 All Rights Reserved