国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > iOS > 正文

iOS UIViewController之間的切換

2019-11-09 14:12:47
字體:
來源:轉載
供稿:網友

我是一枚剛學習iOS的菜鳥,項目中經常會涉及到UIViewController之間的跳轉,常用的就是PushViewController及PResentViewController,現在簡單介紹下他們之間的區別。

PushViewController與PresentViewController的區別

PushViewController是導航的方式跳轉,左右切換,與PopViewController對應,以棧的方式管理,可以把棧理解成一個瓶子,用戶能看到的就是瓶子最上面的東西。而PreSentViewController是模態的方式跳轉,上下切換,與dismissViewController對應。本質上是用一個模態ViewController遮住原來的ViewController,但是可以設置新模態窗口的尺寸,所以不一定會把舊的ViewController完全遮住(如果不設置,默認完全遮住)

假設有兩個頁面A、B,A為ViewController,B為SecondViewController,跳轉的幾種代碼如下:

PushViewController方式

從A->B

SecondViewController *secondViewController = [[SecondViewController alloc] init];[self.navigationController pushViewController:secondViewController animated:YES]; //[self.navigationController setNavigationBarHidden:YES animated:YES]; // 隱藏導航欄從B->A

[self.navigationController popViewControllerAnimated:NO];這里有個坑,在使用PushViewController跳轉時發現沒起作用。不要著急,這是因為self.navigationController為nil,我們只要添加根視圖為NavigationController就行了。解決這個問題有兩個方法:

1.storyboard的方式

在xcode->Editor->Embed In->Navigation Controller

2.代碼的方法,修改AppDelegate.m文件

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];    self.window.backgroundColor = [UIColor whiteColor];    ViewController *viewController = [[ViewController alloc] init];    UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:viewController];    self.window.rootViewController = nav;    [self.window makeKeyAndVisible];    return YES;}

PreSentViewController方式

從A->B
    SecondViewController *secondViewController = [[SecondViewController alloc] init];    [self presentViewController:secondViewController animated:YES completion:^{    }];從B->A
	[self.navigationController dismissViewControllerAnimated:NO completion:^{            }];簡單的跳轉這樣已經可以實現了,但是我在網上看到另外一種跳轉方式,原因說是為了解決從A跳轉到B時導航欄消失的問題,但是我在項目中做懸浮框時也必須這樣寫,還不大理解,先貼出來
    SecondViewController *secondViewController = [[SecondViewController alloc] init];    UINavigationController *navigationcontoller = [[UINavigationController alloc]initWithRootViewController:secondViewController];    navigationcontoller.navigationBar.hidden = YES;    [self presentViewController:navigationcontoller animated:YES completion:^{    }];


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 北川| 绥化市| 合阳县| 贡嘎县| 富顺县| 湟中县| 抚州市| 达孜县| 保亭| 通道| 临高县| 衡东县| 扬中市| 延安市| 建瓯市| 左权县| 靖远县| 纳雍县| 寻乌县| 拉萨市| 利川市| 凉山| 龙游县| 桐梓县| 神池县| 东台市| 永修县| 繁昌县| 黎城县| 义马市| 商水县| 吴川市| 独山县| 台北县| 东明县| 正定县| 衡山县| 尼勒克县| 岳阳市| 万州区| 博罗县|