UIAlertController简单使用

一晚上的研究成果……

之前只使用AppDelegate.m,视图控制器的创建和操作都是在其中完成的,一直报错:

Warning: Attempt to present <UIAlertController: 0x7fa73b586b40> on <AppDelegate: 0x7fa73b71f960> whose view is not in the window hierarchy!

新建类ViewController后,在其中创建视图对象即可。


本段代码实现功能为:点击登陆按钮,对TextField文本框内容验证,验证通过则显示登录成功,否则,登录失败。当点击确定或取消后,关闭键盘。


文件结构、代码如下:


ViewController.h

@interface ViewController : UIViewController<UITextFieldDelegate>

@property (nonatomic, strong) UITextField * userField;
@property (nonatomic, strong) UITextField * passField;


@end


ViewController.m

- (void)viewDidLoad {
    [super viewDidLoad];
    UILabel * lUser = [[UILabel alloc] initWithFrame:CGRectMake(50, 40, 100, 30)];
    UILabel * lPass = [[UILabel alloc] initWithFrame:CGRectMake(50, 90, 100, 30)];
    lUser.text = @"用户名:";
    lPass.text = @"密  码:";
    [self.view addSubview:lUser];
    [lUser release];
    [self.view addSubview:lPass];
    [lPass release];
    
    UITextField * tUser = [[UITextField alloc] initWithFrame:CGRectMake(150, 40, 150, 30)];
    tUser.placeholder = @"请输入用户名";
    tUser.borderStyle = UITextBorderStyleRoundedRect;
    [self.view addSubview:tUser];
    [tUser release];
    
    UITextField * tPass = [[UITextField alloc] initWithFrame:CGRectMake(150, 90, 150, 30)];
    tPass.secureTextEntry = YES;
    tPass.placeholder = @"请输入密码";
    tPass.borderStyle = UITextBorderStyleRoundedRect;
    [self.view addSubview:tPass];
    [tPass release];
    
    UIButton * loginButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [loginButton setTitle:@"登陆" forState:UIControlStateNormal];
    loginButton.frame = CGRectMake(50, 150, 100, 40);
    [loginButton addTarget:self action:@selector(loginClick) forControlEvents:UIControlEventTouchUpInside];
    loginButton.titleLabel.font = [UIFont systemFontOfSize:18];
    [loginButton addTarget:self action:@selector(loginClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:loginButton];
    
    UIButton * registButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [registButton setTitle:@"注册" forState:UIControlStateNormal];
    registButton.frame = CGRectMake(150, 150, 100, 40);
    registButton.titleLabel.font = [UIFont systemFontOfSize:18];
    [self.view addSubview:registButton];
    
    tUser.delegate = self;
    tPass.delegate = self;
    
    self.userField = tUser;
    self.passField = tPass;
    
    // Do any additional setup after loading the view.
}

- (void)loginClick
{
    
    NSString * loginMessage = @"登录失败";
    
    UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"登录信息" message:loginMessage preferredStyle:UIAlertControllerStyleAlert];
    
    if ([self.userField.text isEqualToString:@"123"]) {
        if ([self.passField.text isEqualToString:@"123"]) {
            loginMessage = @"登录成功";
        }
    }
    alert.message = loginMessage;
    UIAlertAction * ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        [self.view endEditing:YES];
    }];
    
    UIAlertAction * cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        [self.view endEditing:YES];
    }];
    
    [alert addAction:ok];
    [alert addAction:cancel];
    
    [self presentViewController:alert animated:YES completion:nil];
    
}

AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    
    ViewController * viewController = [[ViewController alloc] init];
    
    self.window.rootViewController = viewController;
    
    
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}


转载于:https://my.oschina.net/zooyf/blog/493756

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值