溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

如何在子視圖中獲取父視圖的ViewController?//模態(tài)推出

發(fā)布時(shí)間:2020-07-12 09:06:01 來源:網(wǎng)絡(luò) 閱讀:12970 作者:Im劉亞芳 欄目:開發(fā)技術(shù)
子view添加到有controller的父view時(shí),在點(diǎn)擊子view中的什么東西時(shí),如果要調(diào)用父view的controller導(dǎo)航push到一個(gè)新viewController,那么直接用下面的代碼就可以了(本人也遇到這個(gè)問題,以下代碼在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;
}
//點(diǎn)擊提交按鈕
-(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;//彈出時(shí)的動(dòng)畫風(fēng)格 對(duì)彈出目標(biāo)頁面設(shè)置
    [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:備注中的方法已經(jīng)廢棄,被備注2中的presentViewController代替;參數(shù)completion實(shí)現(xiàn)一個(gè)回調(diào),當(dāng)MainViewController的viewDidDisappear調(diào)用之后,該回調(diào)會(huì)被調(diào)用。 
備注3:在MainViewController中調(diào)用self.presentedViewController,返回的是由MainViewController present出的視圖控制器,


以上是代碼實(shí)例 下面是網(wǎng)上抄來無聊時(shí)看看的,補(bǔ)充補(bǔ)充的


一、主要用途

  彈出模態(tài)ViewController是IOS變成中很有用的一個(gè)技術(shù),UIKit提供的一些專門用于模態(tài)顯示的ViewController,如UIImagePickerController等。彈出模態(tài)ViewController主要使用于一下這幾種情形:

  1、收集用戶輸入信息

  2、臨時(shí)呈現(xiàn)一些內(nèi)容

  3、臨時(shí)改變工作模式

  4、相應(yīng)設(shè)備方向變化(用于針對(duì)不同方向分別是想兩個(gè)ViewController的情況)

  5、顯示一個(gè)新的view層級(jí)

  這幾種情形都會(huì)暫時(shí)中斷程序正常的執(zhí)行流程,主要作用是收集或者顯示一些信息。

二、幾個(gè)概念和常用設(shè)置

1、presenting view controller Vs presented view controller

  當(dāng)我們?cè)趘iew controller A中模態(tài)顯示view controller B的時(shí)候,A就充當(dāng)presenting view controller(彈出VC),而B就是presented view controller(被彈出VC)。官方文檔建議這兩者之間通過delegate實(shí)現(xiàn)交互,如果使用過UIImagePickerController從系統(tǒng)相冊(cè)選取照片或者拍照,我們可以發(fā)現(xiàn)p_w_picpathPickerController和彈出它的VC之間就是通過UIImagePickerControllerDelegate實(shí)現(xiàn)交互的。因此我們?cè)趯?shí)際應(yīng)用用,最好也遵守這個(gè)原則,在被彈出的VC中定義delegate,然后在彈出VC中實(shí)現(xiàn)該代理,這樣就可以比較方便的實(shí)現(xiàn)兩者之間的交互。

2、Modal Presentation Styles(彈出風(fēng)格)

  通過設(shè)置presenting VC的modalPresentationStyle屬性,我們可以設(shè)置彈出View Controller時(shí)的風(fēng)格,有以下四種風(fēng)格,其定義如下:

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

  UIModalPresentationFullScreen代表彈出VC時(shí),presented VC充滿全屏,如果彈出VC的wantsFullScreenLayout設(shè)置為YES的,則會(huì)填充到狀態(tài)欄下邊,否則不會(huì)填充到狀態(tài)欄之下。

  UIModalPresentationPageSheet代表彈出是彈出VC時(shí),presented VC的高度和當(dāng)前屏幕高度相同,寬度和豎屏模式下屏幕寬度相同,剩余未覆蓋區(qū)域?qū)?huì)變暗并阻止用戶點(diǎn)擊,這種彈出模式下,豎屏?xí)r跟UIModalPresentationFullScreen的效果一樣,橫屏?xí)r候兩邊則會(huì)留下變暗的區(qū)域。

  UIModalPresentationFormSheet這種模式下,presented VC的高度和寬度均會(huì)小于屏幕尺寸,presented VC居中顯示,四周留下變暗區(qū)域。

  UIModalPresentationCurrentContext這種模式下,presented VC的彈出方式和presenting VC的父VC的方式相同。

  這四種方式在iPad上面統(tǒng)統(tǒng)有效,但在iPhone和iPod touch上面系統(tǒng)始終已UIModalPresentationFullScreen模式顯示presented VC。

3、Modal Transition Style(彈出時(shí)的動(dòng)畫風(fēng)格)

  通過設(shè)置設(shè)置presenting VC的modalTransitionStyle屬性,我們可以設(shè)置彈出presented VC時(shí)場景切換動(dòng)畫的風(fēng)格,其定義如下:

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

  我們可以看到有從底部滑入,水平翻轉(zhuǎn)進(jìn)入,交叉溶解以及翻頁這四種風(fēng)格可選。這四種風(fēng)格在不受設(shè)備的限制,即不管是iPhone還是iPad都會(huì)根據(jù)我們指定的風(fēng)格顯示轉(zhuǎn)場效果。

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

  消失presented VC,我們可以通過調(diào)用以下兩個(gè)函數(shù)中的任何一個(gè)來完成

dismissModalViewControllerAnimated:                 // 將要廢棄,不贊成繼續(xù)使用dismissViewControllerAnimated:completion:


向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI