IOS8之后增加了UIAlertController类,它可以表示UIAlertView和UIActionSheet.它继承自UIViewController。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window.backgroundColor=[UIColor whiteColor];
[self.window makeKeyAndVisible];
ViewController *vc=[[ViewController alloc]init];
UINavigationController *nvc=[[UINavigationController alloc]initWithRootViewController:vc];
self.window.rootViewController=nvc;
return YES;
}
//
// ViewController.m
// AlertVC
//
// Created by City--Online on 15/5/25.
// Copyright (c) 2015年 XQB. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
btn.frame=CGRectMake(100, 100, 50, 50);
[btn setTitle:@"按钮" forState:UIControlStateNormal];
[btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
btn.layer.borderWidth=2.0;
[btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
}
-(void)btnClick:(id)sender
{
// UIAlertController *alert=[UIAlertController alertControllerWithTitle:@"Title" message:@"message" preferredStyle:UIAlertControllerStyleAlert];
//
// [alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
// textField.text=@"name";
// textField.clearsOnBeginEditing=YES;
// }];
// [alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
// textField.text=@"password";
// textField.clearsOnBeginEditing=YES;
// }];
UIAlertController *alert=[UIAlertController alertControllerWithTitle:@"Title" message:@"message" preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *actionOk=[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
NSLog(@"确定");
}];
UIAlertAction *actionCancel=[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
NSLog(@"取消");
}];
[alert addAction:actionOk];
[alert addAction:actionCancel];
[self presentViewController:alert animated:YES completion:nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end