我怎样才能“重置” iPhone应用程序中的标签栏 [英] How can I "reset" the tabbar in an iPhone application

查看:123
本文介绍了我怎样才能“重置” iPhone应用程序中的标签栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个iPhone应用程序:
当你打开应用程序时,你会看到LoginView。如果您登录到应用程序,您会看到一个TabBarController。在第三个和最后一个选项卡中有注销按钮。如果单击,则再次看到LoginView。我的问题是,如果你再次登录,你会看到旧标签栏,所选标签是第三个,而不是一个,并且有一个注销按钮。此外,如果用户使用其他用户登录,请查看上一个用户的旧数据(非常危险)。

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).

以下是代码:
- 委托。 h:

Here's the code: - Delegate.h:

UITabBarController *tabBarController;
LoginViewController *loginView;

- Delegate.m(didFinishLaunchingWithOptions):

- 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(方法):

Delegate.m (methods):

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

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

以下是两个不同viewcontrollers中的两种方法:

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

- (IBAction)login:(id)sender {

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

(退出方法相同)

伙计们,我该如何解决这个痛苦的问题?
所以,这里有一个我想要的应用程序列表:Foursquare,Brightkite等。
每个人都有登录屏幕,标签栏视图和注销按钮。

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.

谢谢@ everyone。

Thanks @ everyone.

推荐答案

对于login-logout-login情况,其中各种事情需要在注销或下次登录时自行重置,我喜欢创建通知,例如NewUserReset。需要将自身重置为原始状态的所有内容都会侦听通知,并运行一种方法来执行所需的任何重置。 tabbar会将按钮标题更改为logout,临时数据结构为nil / zero / release本身等。

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];

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

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.

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

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];

那里的选择器resetForNewUser:只是你想要在通知时运行的方法的名称出去了。该方法如下所示:

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
}

在哪里说//在这里做什么你会添加特定的代码到你的应用程序。例如,您可以添加选项卡栏作为JMUserLogout通知的观察者。在resetForNewUser:方法中,您可以将注销按钮的名称更改为Login。

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.

在一个ViewController或View或数据存储中,它包含来自前一个用户的旧数据,resetForNewUser方法会删除所有数据并将其设置回原来的状态。一个新用户。例如,如果前一个用户在UITextField中输入数据,则会删除文本,yourTextFieldName.text = @;

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 = @"";

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

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];

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

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

这篇关于我怎样才能“重置” iPhone应用程序中的标签栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆