从打开视图上的 ModalView 调用方法

发布于 2024-10-12 04:28:42 字数 318 浏览 4 评论 0原文

基本上我有一个在应用程序启动时加载的 viewController 。在该 VC 中,根据是否有用户数据,我提供一个具有登录名的 ModalView。用户登录后,我可以关闭 modalView,但我想在 opener 上调用一个方法,然后用数据填充表。

我认为从 modalView 我可以做类似的事情,

[self.parentViewController loadInitialData];
[self.dismissModalViewControllerAnimated:YES];

但这不起作用..

有什么建议吗?

Basically I have a viewController that loads at app startup. In that VC, depending on whether there is userdata, I serve up a ModalView with either a login. After the user logs in, I can then dismiss the modalView, but I would like to call a method on the opener that will then populate a table with data.

I thought from the modalView I could do something like

[self.parentViewController loadInitialData];
[self.dismissModalViewControllerAnimated:YES];

but that does not work..

any suggestions?

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

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

发布评论

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

评论(1

烧了回忆取暖 2024-10-19 04:28:42

问题是因为 self.parentViewController 的类型为“UIViewController”,并且您的 -loadInitialData 方法在 UIViewController 中不存在。有几种常见的方法可以解决这个问题......从最简单和最不“正确”到最复杂和最“正确”:

1)首先,您需要将 self.parentViewController 转换为父视图控制器的类型。类似于:

MyParentViewController *parentVC = (MyParentViewController*)self.parentViewController;
[parentVC loadInitialData];

2)您可以向模态视图控制器添加一个属性,该属性显式保留对父视图控制器的引用,然后调用 loadInitialData 执行此操作。

@interface MyModalViewController : UIViewController {
    MyParentViewController *myParentViewController;
}

@property (nonatomic, retain) MyParentViewController *myParentViewController;

然后你可以调用:

[self.myParentViewController loadInitialData];

3)最复杂但最正确的方法是创建一个委托协议,让你的 ParentViewController 实现该协议,并让你的模态视图控制器保留对委托的引用并以这种方式调用。类似这样:

@protocol ManageDataDelegate

- (void) loadInitialData;

@end


@interface MyParentViewController : UIViewController <ManageDataDelegate> { ...


@interface MyModalViewController : UIViewController {
    id<ManageDataDelegate> delegate;
}

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

当您呈现模态视图控制器时,只需设置委托即可。在你的 MyParentViewController 中:

MyModalViewController *vc = [[MyModalViewController alloc] init];
vc.delegate = self;
[self presentModalViewController:vc];

然后在你的模态视图控制器中你可以像这样回调:

[self.delegate loadInitialData];

The problem is because self.parentViewController is of type "UIViewController" and your -loadInitialData method doesn't exist in UIViewController. There are a couple of common ways to solve this problem... from easiest and least "correct" to most complicated and most "correct":

1) First you need to cast your self.parentViewController to the type of your parent view controller. Something like:

MyParentViewController *parentVC = (MyParentViewController*)self.parentViewController;
[parentVC loadInitialData];

2) You can add a property to your modal view controller that explicitly keeps a reference to your parent view controller and then call loadInitialData doing that.

@interface MyModalViewController : UIViewController {
    MyParentViewController *myParentViewController;
}

@property (nonatomic, retain) MyParentViewController *myParentViewController;

Then you can call:

[self.myParentViewController loadInitialData];

3) The most complicated, but most correct way to do it is to create a Delegate protocol, have your ParentViewController implement the protocol and have your modal view controller keep a reference to the delegate and call that way. Something like:

@protocol ManageDataDelegate

- (void) loadInitialData;

@end


@interface MyParentViewController : UIViewController <ManageDataDelegate> { ...


@interface MyModalViewController : UIViewController {
    id<ManageDataDelegate> delegate;
}

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

When you present your modal view controller, just set the delegate. In your MyParentViewController:

MyModalViewController *vc = [[MyModalViewController alloc] init];
vc.delegate = self;
[self presentModalViewController:vc];

Then in your modal view controller you can call back like so:

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