我说一下我做这个的思路吧,登录和注册页面通常只在用户第一次打开app的时候出现一次,登录过一次以后,就要把用户资料保存在plist文件里,当然你可以选择coreData啦,但我觉得plist比较简单。
用户再次打开这个app的时候,就去这个plist文件里找资料,plist不为空,用户就直接进入主页面。
所以在tabBar页面的默认页面也就是第一页呈现出来的那一页加入这几句。
NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *path=[paths objectAtIndex:0];
NSString *filename=[path stringByAppendingPathComponent:@"UserInfo.plist"];//UserInfo.plist为文件名
NSMutableArray *array=[[NSMutableArray alloc] initWithContentsOfFile:filename];
//以上四行是提取plist的内容放入一个Array里面,后面就判断这个array是否为空,为空表示用户没有登录过,loginView就要弹出来,反之就不需要弹出来
if (array.count==0) {
LoginViewController *loginViewController = [[LoginViewController alloc] initWithNibName:@"LoginViewController" bundle:nil];
UINavigationController *LoginNav = [[[UINavigationController alloc] init] autorelease];//这个是NavigationConroller,因为我有做register的页面所以可以从登录页面切换到注册页面
[LoginNav pushViewController:loginViewController animated:NO];
[LoginNav setNavigationBarHidden:NO animated:NO];
[self.tabBarController presentModalViewController:LoginNav animated:NO]; /这里你就可以学一下pushViewController和presentModalViewController两种方法的区别。
[loginViewController release];
}
[array release];
在登录页面加入这个方法
-(void) writeIntoData
{
NSString *userName=[tfUserName text];//tfUserName 和tfPass 是textField
NSString *password=[tfPass text];
NSMutableArray *array=[[NSMutableArray alloc]init];
[array addObject:userName];
[array addObject:password];
NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *path=[paths objectAtIndex:0];
NSString *filename=[path stringByAppendingPathComponent:@"UserInfo.plist"];
[array writeToFile:filename atomically:YES];//把array写入plist文件里
[array release];
}
在viewDidLoad方法加入两个button
UIBarButtonItem *loginButton = [[UIBarButtonItem alloc]
initWithTitle:@"Login"
style:UIBarButtonItemStyleBordered
target:self
action:@selector(LoginAction)];
self.navigationItem.leftBarButtonItem = loginButton;
[loginButton release];
UIBarButtonItem *registerButton = [[UIBarButtonItem alloc]
initWithTitle:@"Register"
style:UIBarButtonItemStyleBordered
target:self
action:@selector(RegisterAction)];
self.navigationItem.rightBarButtonItem = registerButton;
[registerButton release];
然后在LoginAction这个时间里调用下面
[self writeIntoData];
[self dismissModalViewControllerAnimated:YES];//这个是使登录页面dismiss,tabbar就会出现
用户再次打开这个app的时候,就去这个plist文件里找资料,plist不为空,用户就直接进入主页面。
所以在tabBar页面的默认页面也就是第一页呈现出来的那一页加入这几句。
NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *path=[paths objectAtIndex:0];
NSString *filename=[path stringByAppendingPathComponent:@"UserInfo.plist"];//UserInfo.plist为文件名
NSMutableArray *array=[[NSMutableArray alloc] initWithContentsOfFile:filename];
//以上四行是提取plist的内容放入一个Array里面,后面就判断这个array是否为空,为空表示用户没有登录过,loginView就要弹出来,反之就不需要弹出来
if (array.count==0) {
LoginViewController *loginViewController = [[LoginViewController alloc] initWithNibName:@"LoginViewController" bundle:nil];
UINavigationController *LoginNav = [[[UINavigationController alloc] init] autorelease];//这个是NavigationConroller,因为我有做register的页面所以可以从登录页面切换到注册页面
[LoginNav pushViewController:loginViewController animated:NO];
[LoginNav setNavigationBarHidden:NO animated:NO];
[self.tabBarController presentModalViewController:LoginNav animated:NO]; /这里你就可以学一下pushViewController和presentModalViewController两种方法的区别。
[loginViewController release];
}
[array release];
在登录页面加入这个方法
-(void) writeIntoData
{
NSString *userName=[tfUserName text];//tfUserName 和tfPass 是textField
NSString *password=[tfPass text];
NSMutableArray *array=[[NSMutableArray alloc]init];
[array addObject:userName];
[array addObject:password];
NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *path=[paths objectAtIndex:0];
NSString *filename=[path stringByAppendingPathComponent:@"UserInfo.plist"];
[array writeToFile:filename atomically:YES];//把array写入plist文件里
[array release];
}
在viewDidLoad方法加入两个button
UIBarButtonItem *loginButton = [[UIBarButtonItem alloc]
initWithTitle:@"Login"
style:UIBarButtonItemStyleBordered
target:self
action:@selector(LoginAction)];
self.navigationItem.leftBarButtonItem = loginButton;
[loginButton release];
UIBarButtonItem *registerButton = [[UIBarButtonItem alloc]
initWithTitle:@"Register"
style:UIBarButtonItemStyleBordered
target:self
action:@selector(RegisterAction)];
self.navigationItem.rightBarButtonItem = registerButton;
[registerButton release];
然后在LoginAction这个时间里调用下面
[self writeIntoData];
[self dismissModalViewControllerAnimated:YES];//这个是使登录页面dismiss,tabbar就会出现