免费观看又色又爽又黄的小说免费_美女福利视频国产片_亚洲欧美精品_美国一级大黄大色毛片

UIKit框架(9)控制器的modal切換方式(一)-創(chuàng)新互聯

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

創(chuàng)新互聯是一家集網站建設,江漢企業(yè)網站建設,江漢品牌網站建設,網站定制,江漢網站建設報價,網絡營銷,網絡優(yōu)化,江漢網站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強企業(yè)競爭力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯網需求。同時我們時刻保持專業(yè)、時尚、前沿,時刻以成就客戶成長自我,堅持不斷學習、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實用型網站。

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");
}];

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

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

   傳遞方法:

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

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

      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(@"返回");
}];
//源控制器遵循代理協議
@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");
}];

另外有需要云服務器可以了解下創(chuàng)新互聯scvps.cn,海內外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。

文章名稱:UIKit框架(9)控制器的modal切換方式(一)-創(chuàng)新互聯
網站網址:http://m.newbst.com/article10/cejsgo.html

成都網站建設公司_創(chuàng)新互聯,為您提供做網站外貿網站建設、網站設計網站排名、品牌網站建設、企業(yè)建站

廣告

聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯

外貿網站建設