溫馨提示×

溫馨提示×

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

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

UIKit框架(9)控制器的modal切換方式(一)

發(fā)布時間:2020-07-27 20:14:15 來源:網絡 閱讀:504 作者:ymanmeng123 欄目:移動開發(fā)

在UIKit中,每個控制器管理著App中的一個頁面,多頁面的管理方式包括以下幾種:

1)使用模態(tài)方式切換頁面

2)使用導航控制器管理多個頁面

3)使用標簽控制器管理多個頁面


modal,即模態(tài)方式,目的控制器被覆蓋著源控制器,并接受用戶的交互

     默認的動作是:從屏幕的下方彈出


下面介紹modal切換方式實現的三種方式以及頁面之間數據的傳遞

    代理切換

    storyboard的自動型segue

    storyboard的自動型segue


  • 代碼實現控制器的modal切換

切換動作中涉及的兩個控制器

     源控制器:執(zhí)行切換動作的控制器

     目的控制器:被切換并顯示的控制器


源控制器使用以下方法切換目的控制器:

- (void)presentViewController:(UIViewController *)viewControllerToPresent animated:(BOOL)flag completion:(void (^)(void))completion

     viewControllerToPresent參數:目的控制器

     flag參數:動畫使能

     completion參數:切換動作完成時執(zhí)行的代碼

    注意:該方法調用時,源控制器必須已經顯示,不要在viewDidLoad方法中調用


源控制器和目的控制器使用以下方法完成頁面返回的功能:   

- (void)dismissViewControllerAnimated:(BOOL)flag completion:(void (^)(void))completion

     如果是目的控制器調用,會自動交給源控制器去執(zhí)行返回動作,即源控制器和目的控制器都可以執(zhí)行,且效果一樣。


  • 代碼切換:示例1

切換到UIAlertViewController

UIAlertController * ac = [UIAlertController alertControllerWithTitle:@"版本更新?" message:@"檢測到新版本,是否到App Store更新?" preferredStyle:UIAlertControllerStyleAlert];

UIAlertAction * a1 = [UIAlertAction actionWithTitle:@"更新" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
    [[UIApplication sharedApplication] openURL:updateURL];
}];
UIAlertAction * a2 = [UIAlertAction actionWithTitle:@"算了" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
    
}];
[ac addAction:a1];
[ac addAction:a2];

[self presentViewController:ac animated:YES completion:^{
    NSLog(@"modal");
}];

    UIAlertController是UIKit中的控制器,并必須使用modal方式顯示

    UIAlertController內部已經實現了點擊按鈕執(zhí)行dismissViewController...方法

    UIAlertController用于代替UIAlertView及UIActionSheet


  • 代碼切換:示例2

切換到自定義的控制器

//源控制器:執(zhí)行器執(zhí)行切換動作
AMViewContoller * vc = [[UIViewController alloc] init];
[self presentViewController:vc animated:YES completion:^{
    NSLog(@"modal");
}];
//目的控制器:執(zhí)行返回動作
[self dismissViewControllerAnimated:YES completion:^{
    NSLog(@"返回");
}];


  • 代碼切換:控制器間值的傳遞

正向傳遞:源控制器將數據傳遞到目的控制器

     傳遞時機:目的控制器被創(chuàng)建后,目的控制器顯示前

     傳遞方法:

          目的控制器添加屬性

          切換前,為目的控制器的屬性賦值

//目的控制器
@interface AMViewControllerTwo:UIViewController
@property (nonatomic, copy) NSString * str;
@end
@implementation AMViewControllerTwo 
- (void) viewDidLoad
{
    [super viewDidLoad];
    NSLog(@"源控制器:%@", self.str);    
}
@end
//源控制器的切換動作
AMViewControllerTwo * vc = [[AMViewControllerTwo alloc] init];
vc.str = @"這是源控制器AMViewControllerOne給你的數據";
[self presentViewController:vc animated:YES completion:^{
    NSLog(@"modal");
}];


逆向傳遞:目的控制器將數據傳遞到源控制器

     傳遞時機:目的控制器返回前

     傳遞方法:

          目的控制器添加代理屬性并提出代理協(xié)議

          源控制器成為目的控制器的代理并實現代理方法

          dismiss前,調用代理的代理方法并間數據作為方法的參數傳遞

//目的控制器
@class AMViewControllerTwo;
@protocol AMViewControllerTwoDelegate : NSObject 
- (void) viewControllerTwo:(AMViewControllerTwo*) vc dismissWithStr:(NSString *) str;
@end
@interface AMViewControllerTwo
@property (nonatomic, copy) NSString * str;
@property (nonatomic, weak) id<AMViewControllerTwoDelegate> delegate;
@end
//目的控制器執(zhí)行dimiss動作
if ( self.delegate && [self.delegate respondsToSelector:@selector(viewControllerTwo:dismissWithStr:)] ) {
    [self.delegate viewControllerTwo:self dismissWithStr:@"這是目的控制器AMViewControllerOnew還你的數據"];
}
[self dismissViewControllerAnimated:YES completion:^{
    NSLog(@"返回");
}];
//源控制器遵循代理協(xié)議
@interface AMViewControllerOne : UIViewController <AMViewControllerTwoDelegate>
@end
//源控制器實現代理方法
- (void) viewControllerTwo:(AMViewControllerTwo*) vc dismissWithStr:(NSString *) str
{
    NSLog(@"目的控制器:%@", );
}
//源控制器的切換動作
AMViewControllerTwo * vc = [[AMViewControllerTwo alloc] init];
vc.str = @"這是源控制器AMViewControllerOne給你的數據";
vc.delegate = self;
[self presentViewController:vc animated:YES completion:^{
    NSLog(@"modal");
}];


向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI