注意:如果勾选了Use Size Classes,上图中用于登录的Segue的push就得换成show。
#import
"LXViewController.h"
@interface LXViewController ()
@property ( weak , nonatomic ) IBOutlet UITextField *account;
@property ( weak , nonatomic ) IBOutlet UITextField *password;
@property ( weak , nonatomic ) IBOutlet UIButton *LoginBtn;
- ( IBAction )LoginClick;
@end
@implementation LXViewController
- ( void )viewDidLoad {
[ super viewDidLoad ];
[ self . account addTarget : self action : @selector (textChange) forControlEvents : UIControlEventEditingChanged ];
[ self . password addTarget : self action : @selector (textChange) forControlEvents : UIControlEventEditingChanged ];
[ self loadUserData ];
}
- ( void ) textChange{
self . LoginBtn . enabled = self . account . text . length && self . password . text . length ;
}
- ( IBAction )LoginClick {
NSString *account = self . account . text ;
NSString *password = self . password . text ;
if (![account isEqualToString : @"lx" ]) {
return ;
}
if (![password isEqualToString : @"123" ]) {
return ;
}
[ self saveUserData ];
[ self . view endEditing : YES ];
[ self performSegueWithIdentifier : @"login" sender : nil ];
}
- ( void )loadUserData{
NSUserDefaults *defaults = [ NSUserDefaults standardUserDefaults ];
self . account . text = [defaults objectForKey : @"account" ];
self . password . text = [defaults objectForKey : @"password" ];
}
- ( void )saveUserData{
NSUserDefaults *defaults = [ NSUserDefaults standardUserDefaults ];
[defaults setObject : self . account . text forKey : @"account" ];
[defaults setObject : self . password . text forKey : @"password" ];
[defaults synchronize ];
}
@interface LXViewController ()
@property ( weak , nonatomic ) IBOutlet UITextField *account;
@property ( weak , nonatomic ) IBOutlet UITextField *password;
@property ( weak , nonatomic ) IBOutlet UIButton *LoginBtn;
- ( IBAction )LoginClick;
@end
@implementation LXViewController
- ( void )viewDidLoad {
[ super viewDidLoad ];
[ self . account addTarget : self action : @selector (textChange) forControlEvents : UIControlEventEditingChanged ];
[ self . password addTarget : self action : @selector (textChange) forControlEvents : UIControlEventEditingChanged ];
[ self loadUserData ];
}
- ( void ) textChange{
self . LoginBtn . enabled = self . account . text . length && self . password . text . length ;
}
- ( IBAction )LoginClick {
NSString *account = self . account . text ;
NSString *password = self . password . text ;
if (![account isEqualToString : @"lx" ]) {
return ;
}
if (![password isEqualToString : @"123" ]) {
return ;
}
[ self saveUserData ];
[ self . view endEditing : YES ];
[ self performSegueWithIdentifier : @"login" sender : nil ];
}
- ( void )loadUserData{
NSUserDefaults *defaults = [ NSUserDefaults standardUserDefaults ];
self . account . text = [defaults objectForKey : @"account" ];
self . password . text = [defaults objectForKey : @"password" ];
}
- ( void )saveUserData{
NSUserDefaults *defaults = [ NSUserDefaults standardUserDefaults ];
[defaults setObject : self . account . text forKey : @"account" ];
[defaults setObject : self . password . text forKey : @"password" ];
[defaults synchronize ];
}
@end