一晚上的研究成果……
之前只使用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;
}