[英]How can I perform a present modally segue on a view controller that is presented modally?
[英]How can I programmatically present a view controller modally?
編輯:解決這個問題時,我發現從UITabBarController
開始,然后通過AppDelegate.m
的didFinishLaunchingWithOptions:
方法執行登錄驗證要容易得多。
問題:此代碼在我的 AppDelegate.m 中的application didFinishLaunchingWithOptions:
方法中
if([result isEqualToString: @"log"])
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UIViewController *ivc = [storyboard instantiateViewControllerWithIdentifier:@"TabBarControl"];
[(UINavigationController*)self.window.rootViewController pushViewController:ivc animated:NO];
NSLog(@"It's hitting log");
}
它只需要一個 HTTP用戶登錄響應,並將它們帶到我的 TabBarController。 問題是它使用推送而不是模態轉換來顯示頁面。 由於 presentModalViewController 方法已在 iOS7 中棄用或刪除,我如何以編程方式強制進行模態顯示?
舊的presentViewController:animated:
方法已被棄用,我們需要使用presentViewController:animated:completion
來代替。 您現在只需要在方法中添加一個completion
參數 - 這應該有效:
if([result isEqualToString: @"log"])
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UIViewController *ivc = [storyboard instantiateViewControllerWithIdentifier:@"TabBarControl"];
[(UINavigationController*)self.window.rootViewController presentViewController:ivc animated:NO completion:nil];
NSLog(@"It's hitting log");
}
文檔是一個很好的起點 - UIViewController presentViewController:animated
的文檔UIViewController presentViewController:animated
告訴你你需要知道的內容:
presentModalViewController:動畫:
呈現由給定視圖控制器管理給用戶的模態視圖。 (在iOS 6.0中不推薦使用。使用
presentViewController:animated:completion:
而不是。)- (void)presentModalViewController:(UIViewController *)modalViewController animated:(BOOL)animated
由於接受的答案是在Objective-C中,因此您將在Swift中執行此操作。 但是,與接受的答案不同,此答案並未引用導航控制器。
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let secondViewController = storyboard.instantiateViewController(withIdentifier: "secondViewController") as! SecondViewController
self.present(secondViewController, animated: true, completion: nil)
根據需要更改故事板名稱,視圖控制器和ID。
另請參見如何以編程方式關閉視圖控制器 。
在swift 4.2中你可以這樣做。 對於那些想要快速更新版本的答案的人。
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "ExampleViewController")
self.present(controller, animated: true, completion: nil)
在Swift 3/4中
let storyB = UIStoryboard(name: "Main", bundle: nil)
let secondViewController = storyB.instantiateViewController(withIdentifier: "SecondViewControllerID") as! SecondViewController
self.present(secondViewController, animated: true, completion: nil)
let storyB = UIStoryboard(name: "Main", bundle: nil)
let secondViewController =
storyB.instantiateViewController(withIdentifier:
"SecondViewControllerID") as! SecondViewController
self.present(secondViewController, animated: true, completion: nil)
在secondViewController中使用此代碼返回。
self.dismiss(animated: true, completion: nil)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.