您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

iOS Dev(14):NavigationController 基礎範例與 push/present 之別

編輯:iOS 開發綜合

初學者最容易混淆的一對 API 是 pushViewController: 與 presentViewController:。前者屬於 UINavigationController,是「堆疊式」換頁;後者是 UIViewController 自己的方法,是「模態式」彈出。兩者的轉場方向、返回方式,甚至取消方式都完全不同。

AppDelegate:掛上導航控制器

#import <UIKit/UIKit.h>

@class ViewController

@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) ViewController *vc;
@end
#import "AppDelegate.h"
#import "ViewController.h"

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application
    didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.vc = [[ViewController alloc] initWithNibName:nil bundle:nil];
    self.window.rootViewController =
        [[UINavigationController alloc] initWithRootViewController:self.vc];
    [self.window makeKeyAndVisible];
    return YES;
}

@end

只要 rootViewController 是 UINavigationController,navigationController 屬性就會開始可用。

主視圖:兩個按鈕,兩種跳轉

#import "ViewController.h"
#import "LLViewController.h"
#import "MMViewController.h"

@interface ViewController ()
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    UIButton *pushButton = [[UIButton alloc] initWithFrame:CGRectMake(10, 200, 100, 100)];
    [pushButton setTitle:@"Push" forState:UIControlStateNormal];
    [pushButton setBackgroundColor:[UIColor redColor]];
    [pushButton addTarget:self action:@selector(pushMethod:)
         forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:pushButton];

    UIButton *presentButton = [[UIButton alloc] initWithFrame:CGRectMake(10, 400, 100, 100)];
    [presentButton setTitle:@"present" forState:UIControlStateNormal];
    [presentButton setBackgroundColor:[UIColor redColor]];
    [presentButton addTarget:self action:@selector(presentMethod:)
            forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:presentButton];
}

- (void)didReceiveMemoryWarning {
}

// 堆疊式:從右向左推入,可被 navigationController 管理
- (void)pushMethod:(UIButton *)button {
    [self.navigationController pushViewController:[[LLViewController alloc] init]
                                         animated:YES];
}

// 模態式:從下往上彈出,completion 若不做事就傳 ^{}
- (void)presentMethod:(UIButton *)button {
    [self presentViewController:[[MMViewController alloc] init]
                       animated:YES
                     completion:^{}];
}

@end

被 push 的視圖:用 pop 返回

#import "LLViewController.h"

@interface LLViewController ()
@end

@implementation LLViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(10, 200, 100, 100)];
    [button setTitle:@"Pop" forState:UIControlStateNormal];
    [button setBackgroundColor:[UIColor redColor]];
    [button addTarget:self action:@selector(popMethod:)
     forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
}

- (void)didReceiveMemoryWarning {
}

- (void)popMethod {
    [self.navigationController popViewControllerAnimated:YES];
}

@end

被 present 的視圖:用 dismiss 關閉

#import "MMViewController.h"

@interface MMViewController ()
@end

@implementation MMViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(10, 400, 100, 100)];
    [button setTitle:@"Dismiss" forState:UIControlStateNormal];
    [button setBackgroundColor:[UIColor redColor]];
    [button addTarget:self action:@selector(dismissMethod:)
     forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
}

- (void)didReceiveMemoryWarning {
}

- (void)dismissMethod {
    [self dismissViewControllerAnimated:YES completion:^{}];
}

@end

記憶方式很簡單:push 對 pop,都屬於導航堆疊;present 對 dismiss,都屬於模態呈現。用錯一邊,就會出現「關不掉的畫面」或「回不到上一頁」的經典 bug。

轉載請註明出處:https://blog.csdn.net/prevention

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved