首页 > 代码库 > OC与Swift写AlertController
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); }
OC与Swift写AlertController
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。