iOS開發,ios開發教程

來源:互聯網
上載者:User

iOS開發,ios開發教程
傳統的alertView

- (void)alertView{    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"警告" message:@"你的操作時非法的,您要繼續嗎" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"確定", nil];    alert.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;    [alert show];}

傳統的actionSheet
- (void)actionSheet{    UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"警告:你的操作時非法的,您要繼續嗎" delegate:nil cancelButtonTitle:@"取消" destructiveButtonTitle:@"確定"                                              otherButtonTitles:@"關閉", nil];    [sheet showInView:self.view]; }

UIAlertController == UIAlertView + UIActionSheet
- (void)alertController{    // 危險操作:彈框提醒    // 1.UIAlertView    // 2.UIActionSheet    // iOS8開始:UIAlertController == UIAlertView + UIActionSheet    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"警告" message:@"你的操作時非法的,您要繼續嗎" preferredStyle:UIAlertControllerStyleAlert];    // 添加按鈕    __weak typeof(alert) weakAlert = alert;    [alert addAction:[UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {        NSLog(@"點擊了確定按鈕--%@-%@", [weakAlert.textFields.firstObject text], [weakAlert.textFields.lastObject text]);    }]];    [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {        NSLog(@"點擊了取消按鈕");    }]];    [alert addAction:[UIAlertAction actionWithTitle:@"其它" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {        NSLog(@"點擊了其它按鈕");    }]];    // 添加文字框    [alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {        textField.textColor = [UIColor redColor];        textField.text = @"123";        [textField addTarget:self action:@selector(usernameDidChange:) forControlEvents:UIControlEventEditingChanged];        //        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(usernameDidChange:) name:UITextFieldTextDidChangeNotification object:textField];    }];    [alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {        textField.secureTextEntry = YES;        textField.text = @"123";    }];    [self presentViewController:alert animated:YES completion:nil];}- (void)usernameDidChange:(UITextField *)username{    NSLog(@"%@", username.text);}

其他動作
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"警告" message:@"你的操作時非法的,您要繼續嗎" preferredStyle:UIAlertControllerStyleActionSheet];    // 設定popover指向的item    alert.popoverPresentationController.barButtonItem = self.navigationItem.leftBarButtonItem;    // 添加按鈕    [alert addAction:[UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {        NSLog(@"點擊了確定按鈕");    }]];    [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {        NSLog(@"點擊了取消按鈕");    }]];    [self presentViewController:alert animated:YES completion:nil];}// UIAlertControllerStyleActionSheet的使用注意// 1.不能有文字框// 2.在iPad中,必須使用popover的形式展示// Text fields can only be added to an alert controller of style UIAlertControllerStyleAlert// 只能在UIAlertControllerStyleAlert樣式的view上添加文字框

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.