开发者

Protocol and Delegates, passing data from a modal view form to all the tab bar controllers

I'm implementing a tab bar app in which every tab there's a controller. When the app runs, I throw up a modal view with a login form. Once the user logs in, I dismiss this modal view and I would like to pass the username to all the tab bar controllers. I created a Protocol in the modal view controller LoginViewController.h:

@protocol PassUserInfoDelegate <NSObject>
@required
    - (void) passUserInfo: (NSString *)string;
@end

@interface LoginViewController : UIViewController <UIApplicationDelegate> {
    id <PassUserInfoDelegate> delegate;
}

and somewhere in the implementation LoginViewController.h I call [[self delegate] passUserInfo:idJson]; in order to pass the idJson value to the delegates.

Then, in the HomeViewController.h I do:

@interface HomeViewController : UIViewController <PassUserInfoDelegate>

so this controller is a delegate of the Protocol. and in the implementation HomeViewController.m I create the Protocol's method:

- (void) passUserInfo:(NSString *)string
{
    NSLog(@"I got the string = %@", string); 
}

and I assign the HomeViewController as the delegate of the Protocol in LoginViewController:

- (void)viewDidLoad
{
    loginViewController = [[LoginViewController alloc] init];
    [loginViewController setDelegate:self];
    [super viewDidLoad];
}

So far, this is working. I get the idJson string from the Protocol to this class. What I want to do now, is to do the same thing in another class, and I want that class to get the idJson value too. So I created in my Home2ViewController.h the same thing:

 @interface HomeView2Controller : UIViewController <PassUserInfoDelegate>

and created the Protocol method:

 - (void) passUserInfo:(NSString *)string
{
NSLog(@"I got the string = %@", string); 开发者_运维问答
}
    - (void)viewDidLoad
{
    loginViewController = [[LoginViewController alloc] init];
    [loginViewController setDelegate:self];
    [super viewDidLoad];
}

but this the passUserInfo method in this class is never called. What am I doing wrong?

Thanks in advance!!


One of your problems is that your HomeViewController2's view is only loaded the first time you access it so your viewDidLoad method is only going to be called the first time you tap the tab bar item that corresponds to HomeViewController2.

There are a couple of other issues with your code that are somewhat unrelated to your questions but to give you a hint, move the [super viewDidLoad] call to the TOP of your -(void)viewDidLoad method.

Also note that if your viewDidLoad method on HomeViewController2 was being called, you would effectively be creating a new instance of your modal (login) view which would not contain the username info entered in the previous instance. So even if the method was being called, there would be no user info to pass to your controller.

The bottom line is that you don't need to pass the username to all view controllers. Create a User class object (subclass of NSObject) with instance variables for all user attributes you want to keep (i.e. username, email, firstname, lastname etc) and make it a singleton instance (this is optional but will make sure you only have one instance of this class throughout your app lifecycle).

Instantiate the class in your app delegate or wherever you first need to access the User objet (probably in your login view controller), set the attributes you need to set and dismiss the modal view controller.

From here on, you can simply request for the User instance and access its attributes from anywhere in your code, including the different view controller in your tab bar controller.

Here's a simple example of an initialisation method for a singleton User object:

@implementation User

static User *user_ = nil;

+ (User*)sharedInstance
{
    if (user_ == nil)
    {
        user_ = [[super allocWithZone:NULL] init];
    }
    return user_;
}

And here's how you can access the user object after importing the "User.h" header file anywhere in your code:

 User *user = [User sharedInstance];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜