Reputation: 2351
Currently I'm working on an iPhone app that asks the user for login information, verifies this information and presents the user with a table view of their information.
On launch the application delegate launches an empty table view along with the modal view to ask for login credentials. The login credentials consist of a standard username/password field and a button to send the information to a server.
When the users credentials have been verified I want to send a message to the TableView beneath the LoginView that says 'hey, the users credentials have been verified, could you please gather all the data for this user and dismiss the view controller.' I looked at a few tutorials from Apple, specifically the Recipe Table View example, (uses delegation to add recipes) however the method I'm implementing is never executed and was hoping someone could shed some light on my problem.
LoginViewController.h
@protocol GatherDataDelegate;
@interface LoginViewController : UIViewController {
//lots of ivars
id <GatherDataDelegate> delegate;
//more ivars
}
//other properties
@property (nonatomic, assign) id <GatherDataDelegate> delegate;
@end
@protocol GatherDataDelegate <NSObject>
- (void)gatherForUserName:(NSString *)userName gatherForPassword:(NSString *)password;
@end
LoginViewController.m
else if ([dataString isEqualToString:@"Credentials Verified"]){
[self.delegate gatherForUserName:username gatherForPassword:password]
}
TableView.h
@interface RootViewController : UITableViewController <GatherDataDelegate>
//ivar and properties
TableView.h
- (void)gatherForUserName:(NSString *)userName gatherForPassword:(NSString *)password;
NSLog(@"calling gather");
}
It's probably something stupid that I'm missing, like I said, I don't have a lot of experience using delegation, but I see a lot of posts about it. Thanks for any help in advance and taking the time to read this.
Upvotes: 0
Views: 669
Reputation: 64428
Your trying to pass data between two view controller which is bad practice. Instead, you need a data model object that both views communicate with.
First, the login view controller will query the data model for the appropriate login information. If it checks out, the data model will set that user as the current user in the data model such that any future data fetches return data for that user.
Secondly, when the table view controller loads, it simply ask the data model for the information for a user, the data model itself will control which user's data it returns based on the login controller's previous input. the tableview controller only needs to know how to read and display that user info, nothing else.
Such a design is nice and neat and you can add on additional views without having to keep progressively linking all the view controllers together (which will snowball.)
Upvotes: 0
Reputation: 21497
Looks like you did everything right - you created a protocol, implemented it, and sent a message to the delegate... dumb question... are you sure you set the delegate property of the LoginViewController after creating it?
Upvotes: 1