是parentViewController总是一个导航控制器? [英] Is parentViewController always a Navigation controller?

查看:82
本文介绍了是parentViewController总是一个导航控制器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一个星期前在这里抓了我的头,现在有了一点可口可乐的经验,在我的腰带我觉得我有一个关于可能发生的事情。

I was kind of scratching my head at this a week ago, and now with a little bit more Cocoa experience under my belt I feel like I have an inkling as to what might be going on.

我正在创建一个由UINavigationController驱动的应用程序。在AppDelegate中,我使用page 1作为根视图控制器创建了这个类的实例。

I'm making an application that is driven by a UINavigationController. In the AppDelegate, I create an instance of this class, using "page 1" as the Root View Controller.

UINavigationController *aNavigationController = [[UINavigationController alloc] 
     initWithRootViewController:page1ViewController];

现在这里是我遇到的问题。从第1页我想使用一个模态视图控制器滑过界面,然后消失,一旦用户进行了编辑。我使用这样的代码,在Page1ViewController内部:

Now here's where I'm having the problem. From "page 1" I'd like to use a modal view controller that slides over the interface and then disappears once the user has made an edit. I do that using code like this, inside of Page1ViewController:

[self presentModalViewController:myModalViewController animated:YES];

当模态视图控制器不存在时,我想要第1页用户在Modal View控制器中输入的内容。所以,我写了一些这样的代码,它驻留在Modal视图控制器:

When the Modal View Controller is gone, I want a value on "Page 1" to change based on what the user entered in the Modal View Controller. So, I wrote some code like this, which resides in the Modal View Controller:

[self.parentViewController dismissModalViewControllerAnimated:YES];
[self.parentViewController doSomethingPleaseWithSomeData:someData];

对第1页的更新没有发生,我花了很长时间才意识到doSomethingPleaseWithSomeData消息未发送到Page1ViewController,而是发送到Navigation Controller。

The update to page 1 wasn't happening, and it took me a long time to realize that the "doSomethingPleaseWithSomeData" message was not being sent to Page1ViewController, but the Navigation Controller.

使用导航控制器时总是需要这样做吗?我可能配置不当吗?是否有一个简单的方法来获取我想要的View控制器(在这种情况下,Page1ViewController)。

Is this always to be expected when using Navigation Controllers? Did I perhaps configure something improperly? Is there an easy way to get at the View Controller that I want (in this case, Page1ViewController).

推荐答案

使用委托模式来解决你的问题。创建属性

I would recommend using the delegation pattern to solve your problem. Create a property

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

以及相应的通讯协定

@protocol MyModalViewDelegate
@optional
    - (void)myModalViewControllerDidFinish:(MyModalViewController *)aModalViewController;
@end

当用户完成您的视图(例如点击保存按钮)发送此消息:

When the user finishes with your view (e.g. taps the save button), send this message:

if ([self.delegate respondsToSelector:@selector(myModalViewControllerDidFinish:)])
    [self.delegate myModalViewControllerDidFinish:self];

现在,将委托设置为应该管理整个事物的视图控制器,当视图控制器完成时。注意,你需要你的视图控制器来关闭模态视图控制器。但是,在逻辑上,这是有道理的,因为它是提供模态视图控制器的对象的第一位。

Now, set the delegate to the view controller that should manage the whole thing, and it will be notified when the view controller is finished. Note that you'll need your view controller to dismiss the modal view controller. But, logically, that makes sense, since it was the object that presented the modal view controller in the first place.

这是苹果如何解决这个问题,例如,UIImagePickerController和UIPersonPickerController。

This is how Apple solves this problem in, for example, the UIImagePickerController and UIPersonPickerController.

这篇关于是parentViewController总是一个导航控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆