开发者

iPhone simple question: How to call a method from the main-class?

开发者 https://www.devze.com 2023-03-31 08:08 出处:网络
I have following code: LoginViewController *lvc = [[LoginViewController alloc] initWithNibName:@\"LoginViewController\" bundle:nil];

I have following code:

     LoginViewController *lvc = [[LoginViewController alloc] initWithNibName:@"LoginViewController" bundle:nil];
 [self presentModalViewController:lvc animated:false];
 [lvc release]开发者_StackOverflow中文版;

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!


I read a lot about delegate and tried it, but don't get it to work.

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:

@protocol LoginViewControllerDelegate

- (void)loginViewControllerDidFinish;

@end

Which declares a protocol with one method. Then add this between @interface and @end:

@property (nonatomic, assign) id <LoginViewControllerDelegate> delegate;

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:

@interface MainViewController : UIViewController <LoginViewControllerDelegate>

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:

[self.delegate loginViewControllerDidFinish];

And that's it. Any more questions?


Try this:

1) when pushing login view, set some flag in MainViewController

2) in method viewWillAppear in MainViewController check that flag from 1). If it is set then load the initial data and unset flag. Otherwise push LoginView.


You've got an UIApplicationDelegate, and it should have an instance variable that points to the MainViewController. Expose this instance variable via a property, say mainViewController (on your UIApplicationDelegate), and then you can access it like this:

[(MyUIApplicationDelegate *)[[UIApplication sharedApplication] delegate] mainViewController]
0

精彩评论

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