我怎样才能“重置” iPhone 应用程序中的选项卡栏

发布于 2024-10-05 14:12:59 字数 1316 浏览 0 评论 0原文

我有一个 iPhone 应用程序: 当您打开应用程序时,您会看到“LoginView”。如果您登录应用程序,您会看到一个 TabBarController。在第三个也是最后一个选项卡中有“注销”按钮。如果单击,您将再次看到“LoginView”。我的问题是,如果您再次登录,您会看到“旧”选项卡栏,并且所选选项卡是第三个而不是第一个,并且有一个“注销”按钮。另外,如果用户使用不同的用户登录,则会看到前一个用户的旧数据(非常危险)。

这是代码: - Delegate.h:

UITabBarController *tabBarController;
LoginViewController *loginView;

- Delegate.m (didFinishLaunchingWithOptions):

[self.window makeKeyAndVisible];

loginView = [[LoginViewController alloc] init];

if (YES) { /* if the user is not already logged */
    [self.window addSubview:loginView.view];
}

Delegate.m (methods):

- (void)loginComplete {
    [loginView dismissModalViewControllerAnimated:YES];
    [window addSubview:tabBarController.view];
}

- (void)logoutComplete {
    [[tabBarController view] removeFromSuperview];
    [tabBarController release];
    [window addSubview:loginView.view];
}

这是两个不同视图控制器中的两个方法:(

- (IBAction)login:(id)sender {

     TabNavisAppDelegate *delegate =
      (TabNavisAppDelegate *) [[UIApplication sharedApplication] delegate];
  [delegate loginComplete];
  }

注销方法是相同的)

伙计们,我该如何解决这个痛苦的问题? 因此,这里列出了可以实现我想要的功能的应用程序:“Foursquare”、“Brightkite”等。 每个都有一个登录屏幕、一个选项卡视图和一个注销按钮。

谢谢@大家。

I've an iPhone application:
When you open the app you see the "LoginView". If you login into application you see a TabBarController. In the third and last tab there is "Logout" button. If you click you see the "LoginView" again. My problem is that if you login again you see the "old" tabbar and the selected tab is the third and not the one, and there is a "Logout" button. Also, if a user login with a different user, see the old data of the previous user (very dangerous).

Here's the code:
- Delegate.h:

UITabBarController *tabBarController;
LoginViewController *loginView;

- Delegate.m (didFinishLaunchingWithOptions):

[self.window makeKeyAndVisible];

loginView = [[LoginViewController alloc] init];

if (YES) { /* if the user is not already logged */
    [self.window addSubview:loginView.view];
}

Delegate.m (methods):

- (void)loginComplete {
    [loginView dismissModalViewControllerAnimated:YES];
    [window addSubview:tabBarController.view];
}

- (void)logoutComplete {
    [[tabBarController view] removeFromSuperview];
    [tabBarController release];
    [window addSubview:loginView.view];
}

And here's the two methods in two different viewcontrollers:

- (IBAction)login:(id)sender {

     TabNavisAppDelegate *delegate =
      (TabNavisAppDelegate *) [[UIApplication sharedApplication] delegate];
  [delegate loginComplete];
  }

(the logout method is the same)

Guys, how can I solve this painful problem?
So, here's a list of application that do what I want: "Foursquare", "Brightkite" and others.
Each one have a login screen, a tabbar view and a logout button.

Thanks @ everyone.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

腹黑女流氓 2024-10-12 14:12:59

对于登录-注销-登录的情况,所有事情都需要在注销或下次登录时自行重置,我喜欢创建一个通知,例如“NewUserReset”。所有需要将自身重置为原始状态的东西都会侦听通知并运行一个方法来执行所需的任何类型的重置。选项卡栏会将按钮标题更改为注销,临时数据结构为零/零/释放自身等。

它很好地将注销与所有必须完成的事情分离,这样您就不必尝试操纵视图控制器和数据存储并从收到注销点击的控制器查看外观。

发送通知很容易。当用户点击“注销”按钮时,您将发出如下通知:

[[NSNotificationCenter defaultCenter] postNotificationName:@"JMUserLogout" 
                                                object:nil];

您不必将其称为 JMUserLogout,您只需要一个您能识别的字符串和一些东西(我使用了您的姓名缩写)来帮助确保您不会意外发送与您不知道正在侦听的通知同名的通知。

当该通知发出时,任何已向 defaultCenter 注册以侦听@“JMUserLogout”的对象都将执行您选择的任何操作。以下是您的对象注册方式(这应该位于 ViewWillLoad 等某个地方或对象的初始化方法):

[[NSNotificationCenter defaultCenter] addObserver:self 
                                     selector:@selector(resetForNewUser:)
                                         name:@"JMUserLogout"
                                       object:nil];

其中的选择器,resetForNewUser:,只是通知发出时您想要运行的方法的名称。该方法如下所示:

- (void)resetForNewUser:(NSNotification *)notif {
    // DO SOMETHING HERE
}

其中显示 // DO SOMETHING HERE,您将添加特定于您的应用程序的代码。例如,您可以将选项卡栏添加为 JMUserLogout 通知的观察者。在其resetForNewUser: 方法中,您可以将注销按钮的名称更改为Login。

在保存来自先前用户的旧数据的 ViewController 或 View 或数据存储中,resetForNewUser 方法将删除所有这些数据并将其设置回新用户之前应有的方式。例如,如果前一个用户将数据输入到 UITextField 中,您将删除该文本,yourTextFieldName.text = @"";

最后,在释放对象之前,将对象作为观察者删除也很重要。在注册接收通知的每个对象的 Dealloc 方法中,您添加以下内容:

[[NSNotificationCenter defaultCenter] removeObserver:self];

希望这是有意义的。 NSNotificationCenter 的 Apple 文档 解释了更多信息他们提供了几个使用通知的示例应用程序。

For login-logout-login situations where all kinds of things need to reset themselves at the logout or the next login, I like to create a notification, something like "NewUserReset." Everything that needs to reset itself to an original state listens for the notification and runs a method that does whatever kind of resetting it needs. The tabbar would change the button title to logout, temporary data structures nil/zero/release themselves, etc.

It's nicely decouples the logout from all of the things that have to be done so you're not trying to manipulate view controllers and data storage and view appearances from the the controller that received the logout tap.

Sending a notification is easy. When the user taps the Logout button you'll send out a notification like this:

[[NSNotificationCenter defaultCenter] postNotificationName:@"JMUserLogout" 
                                                object:nil];

You don't have to call it JMUserLogout, you just need a string that you'll recognize and something -- I used your initials -- to help ensure you don't accidentally send a notification that has the same name as a notification something you're unaware of is listening for.

When that notification goes out, any object that has registered with the defaultCenter to listen for @"JMUserLogout" will perform any actions you choose. Here's how your object registers (this should be located in some place like ViewWillLoad or the initialization method of the object):

[[NSNotificationCenter defaultCenter] addObserver:self 
                                     selector:@selector(resetForNewUser:)
                                         name:@"JMUserLogout"
                                       object:nil];

The selector there, resetForNewUser:, is just the name of a method you want to run when the notification goes out. That method looks like this:

- (void)resetForNewUser:(NSNotification *)notif {
    // DO SOMETHING HERE
}

Where it says // DO SOMETHING HERE you'll add the code specific to your app. For example, you can add the tab bar as an observer of the JMUserLogout notification. In its resetForNewUser: method you'd change the name of the logout button to Login.

In a ViewController or View or data store that holds old data from the previous user the resetForNewUser method would delete all of that data and set things back to the way they should be fore a new user. For example, if the previous user entered data into a UITextField you would delete the text, yourTextFieldName.text = @"";

Lastly, it's important that you also remove your object as an observer before it's deallocated. In your Dealloc method of each object that registered to receive the notification you add this:

[[NSNotificationCenter defaultCenter] removeObserver:self];

Hopefully that makes sense. The Apple documentation for NSNotificationCenter explains more and they provide several sample apps that use notifications.

狼亦尘 2024-10-12 14:12:59

似乎 tabBarController 没有被释放。 [ 释放前保留计数应为 1] tabBarController 可能会保留在某处。检查它的保留计数。

Seems like tabBarController is not getting released. [ retain count should be 1 before releasing] tabBarController might be retain somewhere. check the retain count of it.

萌面超妹 2024-10-12 14:12:59

如果您想在注销后重置以前用户的旧数据..您所要做的就是重置 UITabBarController 的 viewControllers 属性。

因此,如果您要继承 UITabBarController 的子类,以下代码应该将您的应用程序恢复到其原始状态。

    self.viewControllers = @[self.viewControllerOne, self.viewControllerTwo, self.viewControllerThree];

从文档中:

如果在运行时更改此属性的值,选项卡栏控制器将在安装新视图控制器之前删除所有旧视图控制器。新视图控制器的选项卡栏项目会立即显示,并且不会通过动画调整到位。

If you want to reset the old data from the previous user after you log out.. all you have to do is reset the UITabBarController's viewControllers property.

So if you are subclassing UITabBarController the following code should restore your app to its original state.

    self.viewControllers = @[self.viewControllerOne, self.viewControllerTwo, self.viewControllerThree];

From the documentation:

If you change the value of this property at runtime, the tab bar controller removes all of the old view controllers before installing the new ones. The tab bar items for the new view controllers are displayed immediately and are not animated into position.

○闲身 2024-10-12 14:12:59

tabBarController 对象可能已保留在某处。尝试删除它。

并使用以下代码作为登录、注销方法,

- (void)loginComplete {

    // initialize the tabBarController here. like the following
    if(tabBarController == nil){
    tabBarController = [[UITabBarController alloc] init];

    }
    [loginView dismissModalViewControllerAnimated:YES];
    [window addSubview:tabBarController.view];
}

- (void)logoutComplete {
    [[tabBarController view] removeFromSuperview];
    [tabBarController release];
    tabBarController = nil;
    [window addSubview:loginView.view];
}

这样您的问题就会得到解决。

The tabBarController object may have been retained somewhere. Try to remove that.

And use the following code for Login, Logout methods

- (void)loginComplete {

    // initialize the tabBarController here. like the following
    if(tabBarController == nil){
    tabBarController = [[UITabBarController alloc] init];

    }
    [loginView dismissModalViewControllerAnimated:YES];
    [window addSubview:tabBarController.view];
}

- (void)logoutComplete {
    [[tabBarController view] removeFromSuperview];
    [tabBarController release];
    tabBarController = nil;
    [window addSubview:loginView.view];
}

So that your problem will be solved.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文