1. 程式人生 > >iOS檢視控制器的跳轉方法

iOS檢視控制器的跳轉方法

檢視的跳轉有很多的方式,本文主要選擇常用的方式並將其分為普通的viewController和導航控制器navigationController兩種來進行詳細說明

一、普通檢視控制器跳轉 對於普通的檢視控制器來說,都繼承於viewController,這些檢視控制器能夠進行的跳轉,有兩種,一種是通過presentViewController方法,另外一種是通過storyboard中連線,形成一個segue進行跳轉,但是總的來說,這兩種跳轉都是類似於modal出一個檢視來,並沒有壓棧行為,所以,需要注意的是,這種跳轉不能夠連續進行,換句話說,當從一個檢視控制器跳轉到另外一個檢視控制器,這時候如果還想跳轉,那麼只能是通過dissmissViewController方法跳轉回來,而不能跳轉到一個新的控制器。
1、首先介紹presentViewController方法 在viewController.m中
- (IBAction)presentViewController:(id)sender
{
    // 在storyboard中設定secondViewController的identifier為123
    SecondViewController *secondVc = [self.storyboard instantiateViewControllerWithIdentifier:@"123"];
    // 進行跳轉
    [self presentViewController:secondVc animated:YES completion:nil];
}
很多開發人員在進行跳轉的時候,如果是使用了storyboard結合進行跳轉到第二個檢視控制器,常常會發生這樣的情況,本來是建立了一個類和storyboard中的檢視控制器相關聯,這個storyboard上面是拖拽好了很多UI控制元件的,但是通過這樣的程式碼進行跳轉卻發現跳轉出來的頁面沒有這些空間了,甚至出現了黑屏
- (IBAction)presentViewController:(id)sender
{
    SecondViewController *secondVc = [[SecondViewController alloc]init];
    // 進行跳轉
    [self presentViewController:secondVc animated:YES completion:nil];
}
原因在於:雖然表面上是將storyboard與程式碼想關聯,但是所謂的關聯,是程式碼可以去調整storyboard,而storyboard對於程式碼建立的物件並沒有什麼直接關係,所以,實際上,跳轉到的是一個剛剛建立的檢視控制器物件,而不是在storyboard裡面的檢視控制器 解決方案就是利用storyboard的一個類方法來通過 storyboard ID 這個屬性來獲取到storyboard裡面的控制器(當然,需要在storyboard裡面去設定這個屬性值)

2、利用segue進行跳轉 需要將第一個檢視控制器的一個button拖線連線到第二個檢視控制器,然後在彈出的選項框中選擇present(選擇modal等也是可以的,根據特定的要求進行選擇),這樣點選button就可以進行跳轉了 如果想進行一個傳值的話,那麼需要寫上這些程式碼
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // 需要在storyboard裡面設定segue的identifier
    if ([segue.identifier isEqualToString:@"123"]) {
        // destinationViewController表示segue將要跳轉的檢視控制器
        SecondViewController *secondVc = segue.destinationViewController;
        secondVc.data1 = self.textField1.text;
        secondVc.data2 = self.textField2.text;
    }
}
二、導航控制控制器跳轉
導航控制器是檢視控制器的子類,具有普通檢視控制器沒有的幾個跳轉方法,將他們從文件中摘錄了出來
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated; // Uses a horizontal slide transition. Has no effect if the view controller is already in the stack.
- (UIViewController *)popViewControllerAnimated:(BOOL)animated; // Returns the popped controller.
- (NSArray *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated; // Pops view controllers until the one specified is on top. Returns the popped controllers.
- (NSArray *)popToRootViewControllerAnimated:(BOOL)animated; // Pops until there's only a single view controller left on the stack. Returns the popped controllers.
下面進行解釋說明 1、跳轉到下一個檢視控制器(以壓棧的方式)
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated;
2、跳回到上一個檢視控制器(以出棧的方式)
- (UIViewController *)popViewControllerAnimated:(BOOL)animated;
3、跳回到指定的檢視控制器(以出棧的方式)
- (NSArray *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated;
4、跳回到根檢視控制器(以出棧的方式)
- (NSArray *)popToRootViewControllerAnimated:(BOOL)animated;


相關推薦

iOS檢視控制器方法

檢視的跳轉有很多的方式,本文主要選擇常用的方式並將其分為普通的viewController和導航控制器navigationController兩種來進行詳細說明 一、普通檢視控制器跳轉 對於普通的檢視

iOS首頁控制器隱藏app底部的tabBar

color margin 效果 bottom ios 隱藏 定義 per blog 當你某一天發現app從首頁控制器跳到下n級控制器時,需要隱藏底部tabar時,隱藏不了。 找了半天資料,發現控制器有一個叫hidesBottomBarWhenPushed的屬性,但還不知道怎

tp5中,模板、控制器、js的url方法

false header type 控制 lang dex RM ext TP $this->redirect(‘/Supperman/outerMan‘);$this->display(‘Supperman:outerMan‘);這兩者都只是在當前頁面打開新的

iOS開發UICollectionView 到指定 item 解決方法

今天在自定義控制元件過程中需要解決 collectionView 跳轉到指定 item 的功能,在此記錄下兩種方法和效果差異 方式一:滾動到指定 item [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexP

iOS三種頁面方法

// 第一種通過給頁面設定tag值 // 把tag值為3000的view帶到最前面 也就是顯示在螢幕上; [self.window bringSubviewToFront:[self.window viewWithTag:3000]]; // 把tag值為2000的view

iOS 檢視控制器場詳解

作者:seedante,神祕人士,他的 GitHub。 感謝投稿,原文連結。 前言 螢幕左邊緣右滑返回,TabBar 滑動切換,你是否喜歡並十分依賴這兩個操作,甚至覺得 App 不支援這類操作的話簡直反人類?這兩個操作在大屏時代極大提升了操作效率,其背後的技術便是今天的主題:檢視控制器轉換(View

ios開發:到根檢視到第一個tabbar第一個頁面

跳轉到根檢視並跳轉到第一個tabbar第一個頁面 // 直接跳轉到跟檢視 self.navigationController.tabBarController.hid

iOS 至系統設定頁面整理以及繞過稽核的方法

示例:專案裡面有掃碼功能,當用戶第一次掃碼—選擇不允許訪問相機,再次使用掃碼APP就需要引導使用者到系統的相機頁面開啟相機許可權。類似的功能還有定位、錄音、藍芽、相簿等,這些功能都需要給使用者提示/引導。 根據上述情況,市場上的App有兩種做法: 不做跳轉,給使用者提示;

自定義view,AppDelegate等地方,控制器方法

#pragma mark ---- 跳轉控制器的方法  --- -(void)jumpViewController {     MoreViewController *more = [[MoreViewController alloc]init];     UIApplic

iOS 應用內到系統設置

photo pod style software lin vol ios5 per man 在iOS5下面版本號使用下面方法:【IOS5.1+之後不能使用此方法,iOS8的跳轉方法已找到見下方,iOS7的正在摸索。歡迎大家給出觀點意見】 通過URL Scheme的方

Javascript網址方法

span href www. script div win code bsp cnblogs 第一種: window.location.href="http://www.baidu.com"; 第二種: window.navigate("http://w

微信小程序頁面方法總結

ring 補充 app 程序 space color 使用 attr 微信 微信小程序頁面跳轉目前有以下方法(不全面的歡迎補充): 1. 利用小程序提供的 API 跳轉: // 保留當前頁面,跳轉到應用內的某個頁面,使用wx.navigateBack可以返回到原頁面。/

yii2.0裏的redirect方法

rect 使用 HR AC tro app strong nco htm 在yii2框架裏難免會出現跨控制器跳轉,調用方法等,這就用到了redirect了, 帶參數的 $control=Yii::app()->runController(‘site/show

ios開發之--到指定的TabBarViewController中的某一個VIewController

gpo post mat set ani -- app col uitabbar 比較簡單,也很實用,方法大同小異,僅做記錄,方法的系統記錄如下: [self dismissViewControllerAnimated:YES completion:^{

iOS應用內百度高德蘋果地圖

bool 知識點 coo count value oid bsp lse rec 移動開發經常用到基於位置的一些導航功能,但是對於對導航功能依賴性不強的的應用,我們直接采用應用外跳轉地圖APP即可。 但是應用間跳轉,首先需要設置白名單, 在iOS 9 下涉及到平臺客戶端跳

微信小程序頁面方法和攜帶參數詳解

nload 使用 名稱 屬性 這一 模板語言 UNC 方法 rec 1.頁面跳轉方式 (1)標簽跳轉 open-type的屬性值對應api裏的用法即wx.的用法 1 <navigator url="/page/navigate/navigate?t

vue router方法

his 指定 tail 區別 用法 ont src 2.0 term 1.this.$router.push() 描述:跳轉到不同的url,但這個方法會向history棧添加一個記錄,點擊後退會返回到上一個頁面。 用法: 2.this.$router.replace()

小程式頁面方法

需求:從 index.wxml 頁面,跳轉到 log.wxml 頁面 方法一:WXML頁面實現 <navigator url = "/pages/log/log">跳轉到新頁面</navigator> <navigator url = "/pages/log

laravel框架基本路由及其方法

laravel的路由在routes\web.php下   配置資訊 Route::any('/zc','[email protected]'); 注:zc就是路由的名字在你域名後面加上/zc就是進入了ZcControlle控制器add'方法裡 js&

php實現頁面方法彙總

一共有三種方法實現頁面跳轉,分別利用php提供的header()、html meta標籤、JavaScript指令碼。 header() header()方法通過設定http響應頭中的location域實現跳轉。這種跳轉實現對使用者是不可見的,有瀏覽器直接執行