UIViewController parentViewController 访问属性 [英] UIViewController parentViewController access properties

查看:67
本文介绍了UIViewController parentViewController 访问属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个问题已经被问过好几次了,我确实阅读了关于这个主题的现有帖子,但我仍然需要帮助.

I know this question has been asked several times and I did read existing posts on this topic but I still need help.

我有 2 个 UIViewControllers - 父母和孩子.我使用 presentModalViewController 显示子 UIViewController 如下:

I have 2 UIViewControllers - parent and child. I display the child UIViewController using the presentModalViewController as below:

ChildController *child = 
 [[ChildController alloc] initWithNibName:@"ChildView" bundle:nil];
[self presentModalViewController:child animated:YES];
[child release];

子视图有一个 UIPickerView.当用户从 UIPickerView 选择一个项目并点击完成时,我必须关闭模态视图并在父视图的 UITextField 上显示所选项目.

The child view has a UIPickerView. When user selects an item from UIPickerView and clicks done, I have to dismiss the modal view and display the selected item on a UITextField in the parent view.

在孩子的按钮中点击delegate,我执行以下操作:

In child's button click delegate, I do the following:

ParentController *parent = 
 (ParentController *)[self.navigationController parentViewController];
[parent.myTextField setText:selectedText];
[self dismissModalViewControllerAnimated:YES];

一切正常,没有错误.但我不知道如何加载父视图以显示更新的 UITextField.

Everything works without errors. But I don't know how to load the parent view so that it displays the updated UITextField.

我试过了

[parent reloadInputViews];

没用.请帮忙.

推荐答案

委派是要走的路.我知道有些人可能正在寻找更简单的解决方案,但相信我,我已经尝试过其他人,没有什么比委托更有效的了.因此,任何遇到相同问题的人,请阅读授权并逐步遵循.

Delegation is the way to go. I know some people that may be looking for an easier solution but trust me I have tried others and nothing works better than delegation. So anyone having the same problem, go read up on delegation and follow it step by step.

在您的 subviewcontroller.h - 声明一个协议并在其中声明委托方法.

In your subviewcontroller.h - declare a protocol and declare delegate mthods in it.

@protocol myDelegate
 -(void)clickedButton:(subviewcontroller *)subController;
@end

在你的 subviewcontroller.h 中,在 @interface 中:

In your subviewcontroller.h, within @interface:

id<myDelegate> delegate;
@property (nonatomic, assign) id<myDelegate> delegate;    
NSString *data;
-(NSString *)getData;

在你的 subviewcontroller.m 中,合成 myDelegate.将以下代码添加到要通知 parentviewcontroller 子视图已完成它应该执行的操作的位置:

In your subviewcontroller.m, synthesize myDelegate. Add the following code to where you want to notify your parentviewcontroller that the subview is done doing whatever it is supposed to do:

 [delegate clickedButton:self];

然后处理 getData 以返回您要发送给父视图控制器的任何数据

and then handle getData to return whatever data you want to send to your parentviewcontroller

在你的 parentviewcontroller.h 中,导入 subviewcontroller.h 并使用它的委托

In your parentviewcontroller.h, import subviewcontroller.h and use it's delegate

 #import "subviewcontroller.h"
 @interface parentviewcontroller : VUIViewController <myDelegate>
 {}

在你的 parentviewcontroller.m 中,实现委托方法

In your parentviewcontroller.m, implement the delegate method

 - (void)clickedButton:(subviewcontroller *)subcontroller
 {
   NSString *myData = [subcontroller getData];
   [self dimissModalViewControllerAnimated:YES];
   [self reloadInputViews];
 }

不要忘记内存管理!

这篇关于UIViewController parentViewController 访问属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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