天天看點

iOS之Button以及警告視圖(簡單)

    //建立一個button

    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeSystem];

    button1.frame = CGRectMake(0, 0, 60, 40);

    button1.center = self.window.center;

    [button1 setTitle:@"彈出警告" forState:UIControlStateNormal];

    [button1 addTarget:self action:@selector(handleButtonAction:) forControlEvents:UIControlEventTouchUpInside];

    [self.window addSubview:button1];

    UITextField *textFiel1 = [[UITextField alloc]initWithFrame:CGRectMake(20, 100, CGRectGetWidth(self.window.bounds) - 40, 50)];

    textFiel1.borderStyle =UITextBorderStyleRoundedRect;

    textFiel1.placeholder = @"哈哈哈";

    //修改鍵盤傳回鍵的類型,textFild與鍵盤的聯系緊密

    textFiel1.returnKeyType = UIReturnKeySend;

    //為textField指定代理對象

    //為text 設定了一個代理對象

    textFiel1.delegate = self;

    [self.window addSubview:textFiel1];

    [textFiel1 release];

實作為button添加的方法

- (void)handleButtonAction:(UIButton *)sender

{

    //建立一個警告視圖

    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示資訊"

                                                           message:@"目前是一個警告資訊"

                                                           delegate:self cancelButtonTitle:@"取消"

                                            otherButtonTitles:@"确定",@"哈哈",@"呵呵", nil];

    [alertView show];//顯示警告視圖

    [alertView release];

}

在這段代碼裡,隻實作了幾個小功能,添加了一個button,一個TextFiled,設定鍵盤的傳回鍵形式,為button添加了一個點選方法,觸發方法時,會彈出一個警告視圖;

很簡單~~~