AppDelegate 代碼
(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
ViewController *vc = [ViewController new];
vc.dictUserInfo=@{@"User":[[NSUserDefaults standardUserDefaults] objectForKey:@"User"],
@"Pwd" : [[NSUserDefaults standardUserDefaults] objectForKey:@"Pwd"],
@"IP" : [[NSUserDefaults standardUserDefaults] objectForKey:@"IP"]
};
self.window.rootViewController = vc;
return YES;
}
ViewController.m 代碼
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.txtUser.text =self.dictUserInfo[@"User"];
self.txtPwd.text = self.dictUserInfo[@"Pwd"];
self.txtIP.text = self.dictUserInfo[@"IP"];
}
(IBAction)btnLoginClick:(id)sender {
NSString *user =self.txtUser.text;
NSString *pwd = self.txtPwd.text;
NSString *address = self.txtIP.text;
if (user.length>0&&pwd.length>0&&address.length>0) {
[[NSUserDefaults standardUserDefaults] setObject:user forKey:@"User"];
[[NSUserDefaults standardUserDefaults] setObject:pwd forKey:@"Pwd"];
[[NSUserDefaults standardUserDefaults] setObject:address forKey:@"IP"];
}
else
{
self.alertView.message=@"參數輸入錯誤!";
[self.alertView show];
}
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
你的ViewController 都重新初始化了,當然就沒有了