欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

如何设计 IOS 登录页面的跳转关系

最编程 2024-03-13 21:24:55
...

各种介绍简单的API调用的书很多,但是对于日常开发经常碰到的问题很少有介绍。我这里摘录翻译了一些大家推荐的模式,比如常见的登录窗口需求如下:
-第一次启动应用程序时显示登录屏幕。登录后,转到标签栏控制器的第一个选项卡。
-任何时候他们启动应用程序,检查他们是否登录,并直接跳到根Tab键控制器的第一个选项卡。
-当他们手动点击注销按钮时,显示登录屏幕,并从视图控制器中清除所有数据。
在实现细节上:
-在需要的地方检察登录状态,按需执行注销操作的通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(logoutAccount) name:@"logoutAccount" object:nil];
-在注销时,清除钥匙串中的凭据,然后执行segue再次显示登录视图控制器。
那么通用的流程是:

img_e464cc79221501554f8370284f6a6646.jpe
qpwis.jpg

在appDelegate.m里面的didFinishLaunchingWithOptions

//用户认证: 检查 NSUserDefaults User credential 是否存在,引导页面跳转

if (authenticatedUser) 
{
    self.window.rootViewController = [[UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]] instantiateInitialViewController];        
}
else
{
    UIViewController* rootController = [[UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:@"LoginViewController"];
    UINavigationController* navigation = [[UINavigationController alloc] initWithRootViewController:rootController];

    self.window.rootViewController = navigation;
}

在SignUpViewController.m文件中

- (IBAction)actionSignup:(id)sender
{
    AppDelegate *appDelegateTemp = [[UIApplication sharedApplication]delegate];

    appDelegateTemp.window.rootViewController = [[UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]] instantiateInitialViewController];
}

在文件MyTabThreeViewController.m

- (IBAction)actionLogout:(id)sender {

    // 从 NSUserDefaults 和其他数据中删除用户

    AppDelegate *appDelegateTemp = [[UIApplication sharedApplication]delegate];

    UIViewController* rootController = [[UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:@"LoginViewController"];

    UINavigationController* navigation = [[UINavigationController alloc] initWithRootViewController:rootController];
    appDelegateTemp.window.rootViewController = navigation;

}