如何在子视图中获取父视图的ViewController?//模态推出

子view添加到有controller的父view时,在点击子view中的什么东西时,如果要调用父view的controller导航push到一个新viewController,那么直接用下面的代码就可以了(本人也遇到这个问题,以下代码在IOS7和IOS6.1上都亲测通过)
//获取view的controller
- (UIViewController *)viewController
{
    for (UIView* next = [self superview]; next; next = next.superview) {
        UIResponder *nextResponder = [next nextResponder];
        if ([nextResponder isKindOfClass:[UIViewController class]]) {
            return (UIViewController *)nextResponder;
        }
    }
    return nil;
}
//点击提交按钮
-(void)buttonPress
{
    ElectronController *ec=[[ElectronController alloc]init];
    [[self viewController].navigationController pushViewController: ec animated:YES];
    [ec release];
}
-(void)buttonDown:(id)sender{
    ViewTwo *two = [[ViewTwo alloc]init];
    two.delegate = self;
    two.modalPresentationStyle=UIModalPresentationFullScreen;
    two.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;//弹出时的动画风格 对弹出目标页面设置
    [self presentModalViewController:two animated:YES];
}

//[self presentModalViewController:infoViewController animated:YES];//备注1
[self presentViewController:infoViewController animated:YES completion:^{//备注2
NSLog(@"show InfoView!");
}];

//presentedViewController
NSLog(@"self.presentedViewController=%@",self.presentedViewController);//备注3
}

//备注1、备注2:备注中的方法已经废弃,被备注2中的presentViewController代替;参数completion实现一个回调,当MainViewController的viewDidDisappear调用之后,该回调会被调用。 
备注3:在MainViewController中调用self.presentedViewController,返回的是由MainViewController present出的视图控制器,

以上是代码实例 下面是网上抄来无聊时看看的,补充补充的

一、主要用途

  弹出模态ViewController是IOS变成中很有用的一个技术,UIKit提供的一些专门用于模态显示的ViewController,如UIImagePickerController等。弹出模态ViewController主要使用于一下这几种情形:

  1、收集用户输入信息

  2、临时呈现一些内容

  3、临时改变工作模式

  4、相应设备方向变化(用于针对不同方向分别是想两个ViewController的情况)

  5、显示一个新的view层级

  这几种情形都会暂时中断程序正常的执行流程,主要作用是收集或者显示一些信息。

二、几个概念和常用设置

1、presenting view controller Vs presented view controller

  当我们在view controller A中模态显示view controller B的时候,A就充当presenting view controller(弹出VC),而B就是presented view controller(被弹出VC)。官方文档建议这两者之间通过delegate实现交互,如果使用过UIImagePickerController从系统相册选取照片或者拍照,我们可以发现imagePickerController和弹出它的VC之间就是通过UIImagePickerControllerDelegate实现交互的。因此我们在实际应用用,最好也遵守这个原则,在被弹出的VC中定义delegate,然后在弹出VC中实现该代理,这样就可以比较方便的实现两者之间的交互。

2、Modal Presentation Styles(弹出风格)

  通过设置presenting VC的modalPresentationStyle属性,我们可以设置弹出View Controller时的风格,有以下四种风格,其定义如下:

typedef enum {
    UIModalPresentationFullScreen = 0,
    UIModalPresentationPageSheet,
    UIModalPresentationFormSheet,
    UIModalPresentationCurrentContext,
} UIModalPresentationStyle;

  UIModalPresentationFullScreen代表弹出VC时,presented VC充满全屏,如果弹出VC的wantsFullScreenLayout设置为YES的,则会填充到状态栏下边,否则不会填充到状态栏之下。

  UIModalPresentationPageSheet代表弹出是弹出VC时,presented VC的高度和当前屏幕高度相同,宽度和竖屏模式下屏幕宽度相同,剩余未覆盖区域将会变暗并阻止用户点击,这种弹出模式下,竖屏时跟UIModalPresentationFullScreen的效果一样,横屏时候两边则会留下变暗的区域。

  UIModalPresentationFormSheet这种模式下,presented VC的高度和宽度均会小于屏幕尺寸,presented VC居中显示,四周留下变暗区域。

  UIModalPresentationCurrentContext这种模式下,presented VC的弹出方式和presenting VC的父VC的方式相同。

  这四种方式在iPad上面统统有效,但在iPhone和iPod touch上面系统始终已UIModalPresentationFullScreen模式显示presented VC。

3、Modal Transition Style(弹出时的动画风格)

  通过设置设置presenting VC的modalTransitionStyle属性,我们可以设置弹出presented VC时场景切换动画的风格,其定义如下:

typedef enum {
        UIModalTransitionStyleCoverVertical = 0,
        UIModalTransitionStyleFlipHorizontal,
        UIModalTransitionStyleCrossDissolve,
        UIModalTransitionStylePartialCurl,
} UIModalTransitionStyle;

  我们可以看到有从底部滑入,水平翻转进入,交叉溶解以及翻页这四种风格可选。这四种风格在不受设备的限制,即不管是iPhone还是iPad都会根据我们指定的风格显示转场效果。

4、Dismiss Modal ViewController(消失弹出的VC)

  消失presented VC,我们可以通过调用以下两个函数中的任何一个来完成

dismissModalViewControllerAnimated:                 // 将要废弃,不赞成继续使用dismissViewControllerAnimated:completion:
时间: 2024-07-31 14:30:44

如何在子视图中获取父视图的ViewController?//模态推出的相关文章

MVC 在视图中获取当前的Controller、Action的方式

在视图中获取Controller和Action的方式: Controller: @ViewContext.RouteData.Route.GetRouteData(this.Context).Values["controller"] Action: @ViewContext.RouteData.Route.GetRouteData(this.Context).Values["action"] 或者: Controller: @ViewContext.RouteDat

子视图不响应父视图的手势识别

1.遵守代理 <UIGestureRecognizerDelegate> 2.实现代理方法 // 子视图不响应父视图的手势识别 - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch { if ([touch.view isDescendantOfView:self.TUcalendarHeaderView] || [touch.view isD

jquery 获取子iframe和获取父框架的例子

今天做前端页面设计,用到了iframe,查找资料,整理出来几个用jquery 获取子iframe和获取父框架的例子,用起来非常方便. 在firefox最新版(41)和IE9下测试通过.jquery 版本11.3. //获取子框架coverIframeFormTargetId中ID为attachment_Cover_Image_id的value的值 var attid =$("#coverIframeFormTargetId").contents().find("#attach

转:iframe加载的子页面里面获取父级元素窗口以及元素的高度

iframe里的js要操作父级窗口的dom,必须搞懂几个对象: parent是父窗口(如果窗口是顶级窗口,那么parent==self==top) top是最顶级父窗口(有的窗口中套了好几层frameset或者iframe) self是当前窗口(等价window) 父级页面:index.html <!doctype html> <html> <head> <meta charset="utf-8"> <title>父窗口<

二、vue学习--父元素如何获取子元素的值,子元素如何获取父元素的值

下图是父元素: 下图是子元素,获取父元素的值,使用props定义属性,这样就可以获取到父元素上传过来的set .place.type,拿到值就可以做一些自己的逻辑处理 二.子元素给父元素传值? 下图为子元素将值传递出去的方式,使用emit将值公布给父元素,见下图: 父元素需要接受子元素给的值,在父元素中相应的要有propMethods属性,这个属性可以使用v-on的方式绑定,也可以使用@propMethods的方式,见下图: 上图中的login是处理事件,这里点击按钮时需要子元素来触发事件,所以

Js/Jquery获取iframe中的元素 在Iframe中获取父窗体的元素方法

在web开发中,经常会用到iframe,难免会碰到需要在父窗口中使用iframe中的元素.或者在iframe框架中使用父窗口的元素 js 在父窗口中获取iframe中的元素  1. 格式:window.frames["iframe的name值"].document.getElementByIdx_x("iframe中控件的ID").click(); 实例:window.frames["ifm"].document.getElementByIdx_

js获取iframe中的元素以及在iframe中获取父级的元素(包括iframe中不存在name和id的情况)

第一种情况:iframe中不存在name和id的方法:(通过contentWindow获取) var iframe = document.getElementsByTagName('iframe')[0];var ifr_document = iframe.contentWindow.document;//iframe中的文档内容 或者: var _iframe = document.getElementByIdx_x('iframeId').contentWindow; var _div =_

[Unity3D]Unity3D游戏开发之在Android视图中嵌入Unity视图

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 喜欢我的博客请记住我的名字:秦元培,我博客地址是blog.csdn.net/qinyuanpei. 转载请注明出处,本文作者:秦元培,本文出处:

【QT】在子窗体中控制父窗体

[背景说明]我的主窗体的名字叫做MainWindow,其子窗口是一个叫subDialog的类.我现在想做的是在子窗口的函数中调用父窗口. 在父窗口中打开子窗口 //弹出对话框确定变换的参数 subDialog *dialog = new subDialog(this);//这个地方this非常重要 dialog->setModal(true); dialog->show(); 在上面的代码当中,通过this把父窗口的指针传到了子窗口当中,因此在子窗口中可以使用这个传递的指针控制父窗口做一些事情