#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize User_Name,Password,Login_Label,Register_Label;
- (void)viewDidLoad
{
[super viewDidLoad];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if (![defaults boolForKey:@"Registered"])
{
NSLog(@"No User registered");
// Login_Label.hidden = YES;
}else
{
NSLog(@"User Registered");
//Register_Label.hidden = YES;
[self performSegueWithIdentifier:@"LoggedInPage" sender:self];
}
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)LogIn_Button:(id)sender {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([User_Name.text isEqualToString: [defaults objectForKey:@"userName"] ] && [Password.text isEqualToString: [defaults objectForKey:@"password"]])
{
User_Name.text = nil;
Password.text = nil;
[self performSegueWithIdentifier:@"LoggedInPage" sender:NULL];
}else
{
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"OOPS" message:@"Username or Password is in correct" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *action = [UIAlertAction actionWithTitle:@"ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action)
{
//[self dismissViewControllerAnimated:YES completion:Nil];
}];
[alert addAction:action];
[self presentViewController:alert animated:YES completion:Nil];
}
}
- (IBAction)Register:(id)sender {
if ([User_Name.text isEqualToString:@""] || [Password.text isEqualToString:@""])
{
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"OOPS" message:@"You must complete all fields" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *action = [UIAlertAction actionWithTitle:@"ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action)
{
[self dismissViewControllerAnimated:YES completion:Nil];
}];
[alert addAction:action];
[self presentViewController:alert animated:YES completion:Nil];
} else
{
//[self NewRegisterUser];
}
[self performSegueWithIdentifier:@"RegisterationPage" sender:self];
}
-(void) NewRegisterUser{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:User_Name.text forKey:@"userName"];
[defaults setObject:Password.text forKey:@"password"];
[defaults setBool:YES forKey:@"Registered"];
[defaults synchronize];
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Success" message:@"You have registerd a new user" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *action = [UIAlertAction actionWithTitle:@"Done" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action)
{
[self dismissViewControllerAnimated:YES completion:Nil];
[self performSegueWithIdentifier:@"LoggedInPage" sender:self];
}];
[alert addAction:action];
[self presentViewController:alert animated:YES completion:Nil];
//[self performSegueWithIdentifier:@"LoggedInPage" sender:self];
}
@end
给每个 Vc 一个故事板标识符并执行此操作
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
if ([defaults boolForKey:@"LoggedIn"])
{
UIViewController*vc = [storyboard instantiateViewControllerWithIdentifier:@"homeID"];
UINavigationController*nav = [[UINavigationController alloc] initWithRootViewController:vc];
self.window.rootViewController = nav
}else
{
UIViewController*vc = [storyboard instantiateViewControllerWithIdentifier:@"loginID"];
UINavigationController*nav = [[UINavigationController alloc] initWithRootViewController:vc];
self.window.rootViewController = nav
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句