在ViewController.m裡面增加 -(void)viewDidAppear:(BOOL) animated{}
簡單講就是宣告一個UIAlertController 然後建立風格、輸入訊息 以及增加按鈕
//
// ViewController.m
// Alert Dialog
//
// Created by daniel on 2015/1/26.
// Copyright (c) 2015年 daniel. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void) viewDidAppear:(BOOL)animated
{
//產生一個UIAlertController 風格為 UIAlertcontrollerSyleAlert
//風格可以換成UIAlertControllerSyleActionSheet
UIAlertController *alertController = [UIAlertController
alertControllerWithTitle:@"標題" message:@"訊息" preferredStyle:UIAlertControllerStyleAlert];
//宣告一個確定按鈕
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action){
//按下按鈕要做的事情寫在這
[self dismissViewControllerAnimated:YES completion:nil];
}];
//將確定按鈕加入到UIAlertController中
[alertController addAction:okAction];
//顯示這個Controller 也就是訊息框
[self presentViewController:alertController animated:YES completion:nil];
}
@end
Action 那邊是類似onClick()的時候要做什麼這樣
Style可以選擇兩種
相信大家應該看程式碼就大概瞭解了吧?
就不多解釋了,不過在Objective-C裡面 nil = null這點可能要了解就可以了
END
沒有留言:
張貼留言