OC与Swift写AlertController

    在iOS8以后,alertView和actionSheet,被 alertController所替代.今天用OC和swift,分别写了alertController.给大家做个参考.共勉.
OC:
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    
    UIButton * btn = [UIButton buttonWithType:UIButtonTypeSystem];
    btn.frame = CGRectMake(100, 100, 100, 40);
    btn.backgroundColor = [UIColor yellowColor];
    [btn addTarget:self action:@selector(aa) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:btn];

}
- (void)aa
{
    NSLog(@"%f",[[[UIDevice currentDevice] systemVersion] floatValue]);
    UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"报警" message:@"这是IOS8以后的报警" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction * alertAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
        NSLog(@"11111");
    }];
    [alertController addAction:alertAction];
    [self presentViewController:alertController animated:YES completion:nil];
}
 


Swift:
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        let btn:UIButton = UIButton.buttonWithType(UIButtonType.System) as UIButton;
        btn.backgroundColor = UIColor.yellowColor();
        btn.frame = CGRectMake(100, 100, 100, 40);
        btn.addTarget(self, action: "aa", forControlEvents: UIControlEvents.TouchUpInside);
        self.view.addSubview(btn);
        
        
    }
 
    func aa(){
        var alertController:UIAlertController = UIAlertController(title: "报警", message: "ios和Swifit", preferredStyle: UIAlertControllerStyle.Alert);
        var alertAction:UIAlertAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil);
        alertController.addAction(alertAction);
        self.presentViewController(alertController, animated: true, completion: nil);
    }

你可能感兴趣的:(controller)