在iPhone开发中关于子UIViewController和父UIViewController之间相互调用方法的办法
来源:互联网 发布:阿里云os系统手机 编辑:程序博客网 时间:2024/04/30 01:46
今天在做iphone开发时碰到了一个常用的需求,即在一个viewController中添加另外一个viewController,同时能保证这两个ViewController之间能够相互交互且相互调用方法和函数,在网上查了很多资料,很多开发者说需要使用objective-c变态的delegate,可是我感觉delegate是使用在两个同级之间的UIView比较好,至于能不能使用在父子关系而且是UIVeiwController我也不太清楚,也没有亲自实验过,通过查看SDK的API及其他资料我使用了自己的方法实现了我想要的需求,但是我不知道我的这种方法会不会有致命性的问题,或者会不会有很大的弊端,如果有高人存在的话还望指点一下,我只是一个初学者,下面我将我的方法贴上来:
首先,定义两个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相互交互的方法和思路,如果有什么错误或者弊端还希望大家能够提出来我们共同探讨
- 在iPhone开发中关于子UIViewController和父UIViewController之间相互调用方法的办法
- iPhone开发中两个UIViewController间传递变量和调用方法的处理
- UIViewController和UIViewController之间的交互
- UIViewController和UIViewController之间的交互
- 关于UIViewController和UIView类中方法的使用
- UIViewController之间的通讯方法
- UIViewController 的addChildViewController:(UIViewController *) 方法
- 关于在UIViewController中何时调用[super ... any method name]的问题
- IOS关于UIViewController之间的切换
- IOS关于UIViewController之间的切换
- 关于在UIViewController页面用代码添加UINavigationBar的方法
- iphone--UIViewController/UITabBarController和UINavigationController的结合
- UIViewController方法调用顺序
- UIViewController各种方法生命周期和调用顺序
- 关于 UIViewController 的 viewDidLoad 被调用时机
- UIViewController中loadView和viewDidLoad调用时机
- UIViewController 与加载其中的View之间的相互引用
- iPhone开发之UIViewController中的viewAppear与viewDisappear方法的区别
- [新技术]关于本体和面向对象的区别与联系
- wpa_supplicant 和 802.11g WPA 认证的配置
- 在北京郁闷的半年
- 【转】高性能服务器架构(High-Performance Server Architecture)
- HashMap的设计理念
- 在iPhone开发中关于子UIViewController和父UIViewController之间相互调用方法的办法
- 非iDP(没有$99证书)把app部署到iPhone上的方法
- iphone获取GPS的数据信息并定位
- iPhone的九宫格实现代码(转)
- 我要资源分
- handle(句柄)的作用
- 高效使用JavaEE ORM框架
- filter
- Flex Viewer 解析(二)Flex Viewer源码包结构