iPhone简单问题:如何从主类调用方法?

发布于 2024-12-02 05:00:26 字数 437 浏览 1 评论 0原文

我有以下代码:

     LoginViewController *lvc = [[LoginViewController alloc] initWithNibName:@"LoginViewController" bundle:nil];
 [self presentModalViewController:lvc animated:false];
 [lvc release];

这是从我的 MainViewController 调用的。

现在,当 LoginViewController 将被关闭时(当然这仅在登录正确时发生),我必须调用 MainViewController 中的方法来加载应用程序的初始数据。

我读了很多关于委托的内容并尝试过,但没有让它发挥作用。 有人可以帮我吗?

(如果可以的话,请附上几行代码) 欢迎任何帮助!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夜吻♂芭芘 2024-12-09 05:00:26

我阅读了很多有关委托的内容并进行了尝试,但没有让它发挥作用。

你真正尝试过什么?您的 LoginViewController 必须定义一个简单的委托协议,并且您的 MainViewController 必须符合它。

您需要做的就是在 LoginViewController.h 上方的 @interface 中添加类似的内容:

@protocol LoginViewControllerDelegate

- (void)loginViewControllerDidFinish;

@end

它使用一个方法声明一个协议。然后在@interface和@end之间添加这个:

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

这意味着你的登录视图控制器将有一个名为delegate的属性,它将指向符合其委托协议的任何类的实例(这就是id的意思)(<和之间的东西) >)。不要忘记 .m 文件中的 @synthesize delegate;

现在您需要做的是在 MainViewController.h 内添加到 @interface 行,如下所示:

@interface MainViewController : UIViewController <LoginViewControllerDelegate>

这告诉编译器您的 MainViewController 类符合此 LoginViewControllerDelegate 委托协议。现在在 MainViewController.m 中实现 - (void)loginViewControllerDidFinish; 方法,并在呈现登录视图控制器之前将其委托设置为 self (login.delegate = self;)。当您在登录视图控制器中完成后,在关闭它之前,在委托上调用委托方法:

[self.delegate loginViewControllerDidFinish];

就是这样。还有其他问题吗?

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?

残月升风 2024-12-09 05:00:26

试试这个:

1) 当推送登录视图时,在 MainViewController 中设置一些标志

2) 在 MainViewController 的方法 viewWillAppear 中检查 1) 中的标志。如果已设置,则加载初始数据并取消设置标志。否则推LoginView。

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.

眼眸印温柔 2024-12-09 05:00:26

您已经有了一个 UIApplicationDelegate,并且它应该有一个指向 MainViewController 的实例变量。通过属性公开此实例变量,例如 mainViewController (在您的 UIApplicationDelegate 上),然后您可以像这样访问它:

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

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]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文