如何调用父一级 UiViewController 方法

 我来答
huanglenzhi
推荐于2018-04-26 · 知道合伙人数码行家
huanglenzhi
知道合伙人数码行家
采纳数:117533 获赞数:517216
长期从事计算机组装,维护,网络组建及管理。对计算机硬件、操作系统安装、典型网络设备具有详细认知。

向TA提问 私信TA
展开全部
首先,定义两个UIVeiwController,姑且先命名为ViewControllerParent(父容器)和ViewControllChild(子容器)吧,我们可以通过UIView的

insertSubview方法将子容器添加到父容器中,这点在这里先不用说了

其次,我们先来看一下通过父容器调用子容器中的方法及函数,我先在子容器ViewControllChild和父容器ViewControllerParent中分别写了如下方法:

//该方法是弹出一个警告框

-(void)AlertWindow:(NSString *)transValue{

UIAlertView *alertView=[[UIAlertView
alloc] initWithTitle:transValue
message:transValue delegate:self

cancelButtonTitle:@"OK"
otherButtonTitles:nil];

[alertView show];

[alertView release];

}

由于父容器ViewControllerParent要插入ViewControllChild,因此在ViewControllerParent一定已经定义了ViewControllChild,如下:

@synthesize ViewControllChild;

if
(self.ViewControllChild==nil)
{

ViewControllChild
*ViewControll=[[ViewControllChild alloc]
initWithNibName:@"ViewControllChild"
bundle:nil];

self.ViewControllChild=ViewControll;

[ViewControll release];

}

所以当父容器调用子容器的方法只需要做下面一步即可:

[self.ViewControllChild AlertWindow:@"我是从子容器中弹出来的"];

这就实现了父容器调用子容器中方法;

最后,看一下如何在子容器中调用父容器的方法,我的思路是这样的,在insertSubview时我设置该子容器的父Controller,最后在子容器中通过父Controller来调用方法,因此我在子容器ViewControllChild中添加了一个这样的方法:

//设置当前窗口的父容器

-(void)SetParentView:(UIViewController
*)viewController{

[self setParentViewController:viewController];

}

在ViewControllerParent实现insertSubview前加入下边代码:

[self.ViewControllChild SetParentView:self];

该代码实现了设置父容器

这样在子容器ViewControllChild中通过以下代码就可以调用父容器的方法或者函数了:

[self.parentViewController
AlertWindow:@"我是从父容器中弹出来的"];

以上便是实现ViewController相互交互的方法和思路,如果有什么错误或者弊端还希望大家能够提出来我们共同探讨
苏州蓝晓生物科技有限公司_
2022-08-05 广告
苏州蓝晓生物科技有限公司。标准化核心产品:公司拥有完整的琼脂糖介质、葡聚糖介质、聚甲基丙烯酸酯介质生产线,年产分离介质50000L,产品质量稳定并达到国际领先水平。核心优势:公司核心技术人员拥有近二十年不同基质的基球开发和官能化的丰富技术经... 点击进入详情页
本回答由苏州蓝晓生物科技有限公司_提供
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式