ios 基于情节提要的模态登录屏幕示例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16351348/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 23:26:24  来源:igfitidea点击:

Example for login screen modally based on storyboard

iosstoryboardmodal-dialog

提问by aVC

I am learning ios/ xcode and at a roadblock.

我正在学习 ios/xcode 并且遇到了障碍。

I have a tabbarcontroller+navigation based design. I need to present a login screen if user is not logged in. Here is the basic heirarchy. The login page needs a navigationBar (as the tutorial I followed puts a "Go" button on the bar.

我有一个基于 tabbarcontroller+navigation 的设计。如果用户未登录,我需要显示一个登录屏幕。这是基本的层次结构。登录页面需要一个导航栏(因为我遵循的教程在栏上放置了一个“开始”按钮。

LoginController: (LTController.h,.m)

Main View:TabBarController>
                   NavigationController>View1>View1a
                   NavigationController>View2

Storyboard layout

故事板布局

I read lot of posts here on modal view, delegate method, etc. Most of them are code snippets which unfortunately are a bit over the head for my beginner level.

我在这里阅读了很多关于模态视图、委托方法等的帖子。其中大部分是代码片段,不幸的是,对于我的初学者水平来说有点过头了。

Would appreciate a simple explanation as to how to achieve this. Espacially a note on which files needs changes would be great.

将不胜感激关于如何实现这一点的简单解释。特别是关于哪些文件需要更改的注释会很棒。

thanks

谢谢

采纳答案by Kumar KL

Here is scenario . Its so simple . I just hope that it will be useful.

这是场景。就这么简单。我只是希望它会有用。

enter image description here

在此处输入图片说明

For the UITableBarController give a name for identity to storyboard identer image description here

对于 UITableBarController,为故事板 id指定一个身份名称在此处输入图片说明

Then in your ViewController class file You have the authentication credentials right >.? Do some stuff over there for authentication . then follow this code . It works fine

然后在您的 ViewController 类文件中您拥有身份验证凭据的权利 >.? 在那里做一些事情进行身份验证。然后按照这个代码。它工作正常

- (IBAction)Login:(id)sender {

    if(authenticated)  // authenticated---> BOOL Value assign True only if Login Success
        {
            UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
            UITabBarController *obj=[storyboard instantiateViewControllerWithIdentifier:@"tab"];
            self.navigationController.navigationBarHidden=YES;
            [self.navigationController pushViewController:obj animated:YES];
        } 

回答by CocoaEv

it looks like you are off to a good start. Since you have a tabbar design, you have to make a choice on how to present the login page and when you will do that.

看起来你有一个好的开始。由于您有一个标签栏设计,您必须选择如何显示登录页面以及何时执行此操作。

you have to either present it before the tabbar is shown, or put logic in your first view controller to initiate the login process. There are other ways as well, but they get more complicated and I wanted to give you basic choices right now.

您必须在显示选项卡栏之前显示它,或者将逻辑放在您的第一个视图控制器中以启动登录过程。还有其他方法,但它们变得更加复杂,我现在想给你一些基本的选择。

Here is the general concept I'd recommend.

这是我推荐的一般概念。

a) create a persistent storage variable somewhere to determine if a user is logged in or not.

a) 在某处创建一个持久存储变量以确定用户是否已登录。

b) add a check for this flag in the View will load method of the first view controller attached to your tabbar.

b) 在附加到标签栏的第一个视图控制器的视图将加载方法中添加对此标志的检查。

c) present a modal login page directly from the view controller. if they login, great dismiss it, if not, they are stuck on the modal page.

c) 直接从视图控制器呈现一个模态登录页面。如果他们登录,很好地关闭它,否则,他们会被卡在模态页面上。

so, here is basically how to do that:

所以,这里基本上是如何做到这一点:

for purposes of explaining, I'll going to call your first view controller - first tab on your tabbar controller - fviewController - ok?

出于解释的目的,我将调用您的第一个视图控制器 - 标签栏控制器上的第一个选项卡 - fviewController - 好吗?

in fviewController.m

在 fviewController.m 中

-(void)viewDidLoad {

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    if ([[defaults objectForKey:@"loggedIn"]boolValue]) {
       NSLog(@"user is logged in - do nothing");
    }
   else {

       NSLog(@"User is not logged in");
       [self  performSegueWithIdentifier:@"LoginPage" sender:self];
   }

}

a couple more points it looks like you are using storyboards and segues. In that case, you would do the following:

还有几点,看起来您正在使用故事板和转场。在这种情况下,您将执行以下操作:

  • create a new view controller for your login page
  • control drag a segue connection to it from the first view controller in your tabbar
  • identify the segue as "modal"
  • crate a new view controller class for the login view controller
  • present your view and manage your authentication
  • if the user is logged in, you need to store that back to the NSUserDefaults Note: if you have multiple users or other schemes, you may want to modify the single value I showed you in the example go track status for current user. Also: if you have logout code, you need to set the flag correctly. Also: if users are going to login and logout frequently, then use view will appear instead of view did load.
  • 为您的登录页面创建一个新的视图控制器
  • 控制从标签栏中的第一个视图控制器拖动一个 segue 连接到它
  • 将 segue 标识为“模态”
  • 为登录视图控制器创建一个新的视图控制器类
  • 展示您的视图并管理您的身份验证
  • 如果用户已登录,您需要将其存储回 NSUserDefaults 注意:如果您有多个用户或其他方案,您可能需要修改我在示例中显示的单个值 go track status for current user。另外:如果您有注销代码,则需要正确设置标志。另外:如果用户要经常登录和注销,那么会出现使用视图而不是视图确实加载。

To flip the status:

要翻转状态:

   NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
   [defaults setValue:[NSNumber numberWithBool:YES] forKey:@"loggedIn"]; //in
   [defaults setValue:[NSNumber numberWithBool:NO] forKey:@"loggedIn"]; //out

   do this in your login controller

To dismiss the modal view. Technically you should use a delegate callback to do this, but if you are trying to keep things simple, this should be ok

关闭模态视图。从技术上讲,您应该使用委托回调来执行此操作,但是如果您想保持简单,这应该没问题

       [self dismissViewControllerAnimated:YES completion:^{

        }];

So your logic would be like this - did they login? Yes, then set YES status for logged in and then dismiss. If they dod not login, do nothing. They are stuck.

所以你的逻辑是这样的——他们登录了吗?是的,然后为登录设置 YES 状态,然后关闭。如果他们不登录,则什么也不做。他们被卡住了。

Finally, if you need to setup your login controller, you would use the method: prepareForSegue ... to initialize variables before the segue occurs. You probably have read about it if you are doing some tutorials.

最后,如果您需要设置登录控制器,您可以使用方法:prepareForSegue ... 在 segue 发生之前初始化变量。如果您正在做一些教程,您可能已经阅读过它。

Well ... hope that helps. It is a very basic approach. If you get that working, you can continue to add more security and capabilities to it as you go.

嗯...希望有帮助。这是一个非常基本的方法。如果你能成功,你可以继续为它添加更多的安全性和功能。

best of luck.

祝你好运。