简体   繁体   English

覆盖UINavigationController中的后退按钮

[英]Override back button in UINavigationController

I'm fairly new to iOS development and am working on my first app. 我是iOS开发的新手,正在开发我的第一个应用程序。 My app has 3 main views, the first is the login screen (which allows users to login to the web service), the second is a submit screen (which allows users to submit content to the web service) and the last is the settings view (which among other things allows users to log out of the app). 我的应用程序具有3个主要视图,第一个是登录屏幕(允许用户登录Web服务),第二个是提交屏幕(允许用户向Web服务提交内容),最后一个是设置视图(除其他外,它还允许用户注销该应用程序)。

I'm storing the user credentials in Keychain as good practice, and so that the user can stay logged in and usually when they open the app they can just go to the submit content page. 我将用户凭据存储在钥匙串中,这是一种很好的做法,这样用户可以保持登录状态,通常在打开应用程序时,他们可以直接进入提交内容页面。 As such the submit view is essentially my main view. 因此,提交视图本质上是我的主要视图。

To access the settings I have a small gear button on the submit view that will take a user to the settings page. 要访问设置,我在提交视图上有一个小齿轮按钮,它将带用户进入设置页面。 On the settings page I have a log out button that deletes your username and password from keychain. 在设置页面上,我有一个注销按钮,可从钥匙串中删除您的用户名和密码。

So if you've logged out of the app I want the back button to take you to the login view if not it should take you back to the submit view, but I can't seem to control what the back button takes the user. 因此,如果您已退出该应用程序,则希望后退按钮将您带到登录视图,否则,您应返回到提交视图,但我似乎无法控制后退按钮将用户带到什么位置。

I know Apples guidelines say not to do something like this but this seems like an obvious design choice. 我知道苹果公司的准则说不要做这样的事情,但这似乎是一个显而易见的设计选择。 How would I override the back button's action on the navigation bar? 如何覆盖导航栏上的后退按钮?

Simply hide the navigation bar and put your custom navigation bar and write what ever name you want to button and provide back button action using [self.navigationController popViewControllerAnimated: YES]; 只需隐藏导航栏,然后放置您自定义的导航栏,并使用[self.navigationController popViewControllerAnimated: YES];写下您想要按的名称并提供后退按钮动作[self.navigationController popViewControllerAnimated: YES]; or choose whatever view controller your want.. 或选择您想要的任何视图控制器。

OR 要么

You can simply create Subclass of NavigationController and override method popViewControllerAnimated: 您可以简单地创建NavigationController的子类并override方法popViewControllerAnimated:

It would be easy for you to save user information in user default instead of keychain. 您很容易将用户信息保存在用户默认设置中,而不是钥匙串中。 When user login add this 用户登录时添加此

[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"UserAlreadyRegister"];

When user logout add this 用户注销时添加此

[[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"UserAlreadyRegister"];

And the put following code in view did appear of your submit view 并将以下代码放入视图中确实出现在您的提交视图中

-(void)viewDidAppear:(BOOL)animated{
    BOOL alreadyRegister = [[NSUserDefaults standardUserDefaults] boolForKey:@"UserAlreadyRegister"];
    if (alreadyRegister){
        [self.navigationController popViewControllerAnimated:YES];
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM