【iOS基礎控制項 -15 - 3 】Modal

來源:互聯網
上載者:User

標籤:

A.概念1.也可以用來切換控制器2.如ActionSheet 除了push之外,還有另外一種控制器的切換方式,那就是Modal任何控制器都能通過Modal的形式展示出來Modal的預設效果:新控制器從螢幕的最底部往上鑽,直到蓋住之前的控制器為止

以Modal的形式展示控制器
- (void)presentViewController:(UIViewController *)viewControllerToPresent animated: (BOOL)flag completion:(void (^)(void))completion

關閉當初Modal出來的控制器
- (void)dismissViewControllerAnimated: (BOOL)flag completion: (void (^)(void))completion;  B.使用1.使用代碼展示modal控制器: presentViewController隱去modal介面:dismissViewController 源控制器和modal控制器的關係
  • rootViewController一直都是源控制器
  • 系統會自己替換view
  • 源控制器中有一個屬性指向modal控制器:presentedViewController(所以modal控制器才不會被銷毀)
 使用NavigationController封裝一下modal控制器,添加上一個導航條 AppDelegate:
 1 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 2     // Override point for customization after application launch. 3     4     // 設定window 5     self.window = [[UIWindow alloc] init]; 6     self.window.backgroundColor = [UIColor grayColor]; 7     self.window.frame = [[UIScreen mainScreen] bounds]; 8     [self.window makeKeyAndVisible]; 9    10     // 建立一個UIView11     UIViewController *viewController = [[UIViewController alloc] init];12     viewController.view.backgroundColor = [UIColor redColor];13     UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(100, 100, 200, 50)];14     [button setTitle:@"彈出modal" forState:UIControlStateNormal];15     [button addTarget:self action:@selector(popToModal) forControlEvents:UIControlEventTouchUpInside];16    17     [viewController.view addSubview:button];18     self.window.rootViewController = viewController;19    20     return YES;21 }22 23 - (void) popToModal {24     UIViewController *viewController = self.window.rootViewController;25    26     // 再建立一個UIView用來切換27     UIViewController *modalViewController = [[UIViewController alloc] init];28     modalViewController.view.backgroundColor = [UIColor blueColor];29    30     // 建立一個NavigationController來封裝31     UINavigationController *naviController = [[UINavigationController alloc] initWithRootViewController:modalViewController];32     modalViewController.navigationItem.title = @"藍色";33    34     // 切換到navigationController35     [viewController presentViewController:naviController animated:YES completion:^{36         NSLog(@"完成modal切換");37     }];38 }
   2.使用storyboard(1)拖入相應控制器並連線 使用“modal”進行跳轉,而不是之前的“push” (2)關閉modal,回跳GreenViewController:
1 - (IBAction)goback:(UIBarButtonItem *)sender {2     // 關閉modal3     // 1.可以使用自己4 //    [self dismissViewControllerAnimated:YES completion:nil];5    6     // 2.也可以使用上一層的navigation controller7     [self.navigationController dismissViewControllerAnimated:YES completion:nil];8 }
 (3)關於modal連線的資料轉送問題a.順傳:使用segue的控制器“紅”控制器傳送字串給“綠”控制器:
 1 /* 2 * 在這裡傳輸資料給modal控制器 3 * 順傳,使用控制器 4 */ 5 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 6     // 1.取得modal轉向的controller,這裡是中間的navigation controller 7     UINavigationController *nvController =  (UINavigationController *)segue.destinationViewController; 8     9     // 2.取得modal最終顯示的controller, 就是最後的UIViewController10     GreenViewController *greenController = (GreenViewController *)nvController.topViewController;11    12     // 3.傳輸資料13     greenController.name = @"我就是一個modal視窗";14 }
  b.逆傳:使用代理“綠”控制器傳送資料給“紅”控制器

【iOS基礎控制項 -15 - 3 】Modal

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.