iOS编程------UIAlertController

来源:互联网 发布:八爪鱼采集器mac版 编辑:程序博客网 时间:2024/04/27 21:54
////  AppDelegate.h//  UIAlertViewController////  Created by l on 15/9/4.//  Copyright (c) 2015年 . All rights reserved.//#import <UIKit/UIKit.h>@interface AppDelegate : UIResponder <UIApplicationDelegate>@property (strong, nonatomic) UIWindow *window;@end////  AppDelegate.m//  UIAlertViewController////  Created by l on 15/9/4.//  Copyright (c) 2015年 . All rights reserved.//#import "AppDelegate.h"@interface AppDelegate ()@end@implementation AppDelegate- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    // Override point for customization after application launch.    return YES;}////////////////////////////////////////////////////////////////////  ViewController.h//  UIAlertViewController////  Created by l on 15/9/4.//  Copyright (c) 2015年 . All rights reserved.//#import <UIKit/UIKit.h>@interface ViewController : UIViewController@end////  ViewController.m//  UIAlertViewController////  Created by l on 15/9/4.//  Copyright (c) 2015年 . All rights reserved.//#import "ViewController.h"@interface ViewController ()@property (nonatomic, strong) UIAlertController *alertController;@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];    /*    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"AlertController" message:@"this is a alertConttoller" preferredStyle:(UIAlertControllerStyleAlert)];    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:nil];    //添加事件    [alertController addAction:cancel];    //模态显示alertController    //presentViewController: 为模态显示方式    //animated: 为模态显示的时候是否做动画    //completion: 为显示出来后,要执行的block块    [self presentViewController:alertController animated:YES completion:nil];//写在这里是错误的!!!  */    _alertController = [UIAlertController alertControllerWithTitle:@"AlertController" message:@"this is a alertController" preferredStyle:(UIAlertControllerStyleAlert)];    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:nil];    [_alertController addAction:cancel];    // Do any additional setup after loading the view, typically from a nib.}//一个controller 什么时候它的view才会被显示出来呢?//当controller.view被添加到父视图上面的时候//先执行viewWillAppear 后执行viewDidAppear// 因此 当视图被显示出来的时候,执行的是 vieDidAppear//同时只能给view设置一个rootVC,如果把一个controller的展示写在rootVC的viewDidLoad 和loadView里面,会产生逻辑混乱,window不知道该加载哪个根视图控制器.- (void)viewDidAppear:(BOOL)animated{    [self presentViewController:_alertController animated:YES completion:nil];}- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}@end
0 0
原创粉丝点击