I have following code:
LoginViewController *lvc = [[LoginViewController alloc] initWithNibName:@"LoginViewController" bundle:nil];
[self presentModalViewController:lvc animated:false];
[lvc release];
That is called from my MainViewController.
Now, when the LoginViewController will be dismissed (of course this only happens when the login is correct) I must call a method in my MainViewController to load the initial data for my app.
I read a lot about delegate and tried it, but don’t get it to work.
Could someone help me please?
(if possible, please with a few lines of code)
Any help is welcome!
What have you tried really? Your LoginViewController must define a simple delegate protocol, and your MainViewController must conform to it.
All you need to do is add something like this in LoginViewController.h above @interface:
Which declares a protocol with one method. Then add this between @interface and @end:
Which means your login view controller will have a property called delegate which will point to an instance of any class (that’s what id means) that conforms to it’s delegate protocol (the thing between < and >). Don’t forget to
@synthesize delegate;inside .m file.Now what you need to do is inside MainViewController.h add to @interface line like this:
Which tells the compiler your MainViewController class conforms to this LoginViewControllerDelegate delegate protocol. Now implement the
- (void)loginViewControllerDidFinish;method inside MainViewController.m and before presenting the login view controller modally set it’s delegate to self (login.delegate = self;). When you are done inside your login view controller, before you dismiss it, call the delegate method on your delegate:And that’s it. Any more questions?