Edit: When solving this problem, I found that it is much easier to start with your UITabBarController
, then perform login validation via your AppDelegate.m
's didFinishLaunchingWithOptions:
method.
编辑:当解决这个问题时,我发现从UITabBarController开始更容易,然后通过AppDelegate.m的didFinishLaunchingWithOptions:方法执行登录验证。
Question: This code is in the the application didFinishLaunchingWithOptions:
method in my AppDelegate.m
问题:此代码位于我的AppDelegate.m中的应用程序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");
}
It simply takes an HTTP response for the user being logged in, and takes them to my TabBarController. The problem is that it's using a push rather than a modal transition to display the page. Since presentModalViewController method is deprecated or deleted in iOS7, how can I programmatically force a modal presentation?
它只是为登录的用户提供HTTP响应,并将它们带到我的TabBarController。问题是它使用推送而不是模态转换来显示页面。由于在iOS7中不推荐使用presentModalViewController方法,因此如何以编程方式强制进行模式演示?
编辑:)
4 个解决方案
#1
28
The old presentViewController:animated:
method has been deprecated, we need to use presentViewController:animated:completion
instead. You now simply have to add a completion
parameter to the method - this should work:
旧的presentViewController:animated:方法已被弃用,我们需要使用presentViewController:animated: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");
}
The docs are a good place to start - the docs for UIViewController presentViewController:animated
tell you exactly what you need to know:
文档是一个很好的起点 - UIViewController presentViewController的文档:动画告诉你你需要知道的确切内容:
presentModalViewController:animated:
Presents a modal view managed by the given view controller to the user. (Deprecated in iOS 6.0. Use
presentViewController:animated:completion:
instead.)呈现由给定视图控制器管理给用户的模态视图。 (在iOS 6.0中不推荐使用。使用presentViewController:animated:completion:而不是。)
- (void)presentModalViewController:(UIViewController *)modalViewController animated:(BOOL)animated
#2
17
Swift
Since the accepted answer is in Objective-C, this is how you would do it in Swift. Unlike the accepted answer, though, my answer does not reference the navigation controller.
由于接受的答案是在Objective-C中,因此您将在Swift中执行此操作。但是,与接受的答案不同,我的答案没有引用导航控制器。
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let secondViewController = storyboard.instantiateViewControllerWithIdentifier("secondViewControllerId") as! SecondViewController
self.presentViewController(secondViewController, animated: true, completion: nil)
Change the storyboard name, view controller, and id as needed.
根据需要更改故事板名称,视图控制器和ID。
See also how to dismiss a view controller programmatically.
另请参见如何以编程方式关闭视图控制器。
#3
0
In Swift 3/4
在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)
#4
0
let storyB = UIStoryboard(name: "Main", bundle: nil)
let secondViewController =
storyB.instantiateViewController(withIdentifier:
"SecondViewControllerID") as! SecondViewController
self.present(secondViewController, animated: true, completion: nil)
In the secondViewController use this code to go back.
在secondViewController中使用此代码返回。
self.dismiss(animated: true, completion: nil)
#1
28
The old presentViewController:animated:
method has been deprecated, we need to use presentViewController:animated:completion
instead. You now simply have to add a completion
parameter to the method - this should work:
旧的presentViewController:animated:方法已被弃用,我们需要使用presentViewController:animated: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");
}
The docs are a good place to start - the docs for UIViewController presentViewController:animated
tell you exactly what you need to know:
文档是一个很好的起点 - UIViewController presentViewController的文档:动画告诉你你需要知道的确切内容:
presentModalViewController:animated:
Presents a modal view managed by the given view controller to the user. (Deprecated in iOS 6.0. Use
presentViewController:animated:completion:
instead.)呈现由给定视图控制器管理给用户的模态视图。 (在iOS 6.0中不推荐使用。使用presentViewController:animated:completion:而不是。)
- (void)presentModalViewController:(UIViewController *)modalViewController animated:(BOOL)animated
#2
17
Swift
Since the accepted answer is in Objective-C, this is how you would do it in Swift. Unlike the accepted answer, though, my answer does not reference the navigation controller.
由于接受的答案是在Objective-C中,因此您将在Swift中执行此操作。但是,与接受的答案不同,我的答案没有引用导航控制器。
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let secondViewController = storyboard.instantiateViewControllerWithIdentifier("secondViewControllerId") as! SecondViewController
self.presentViewController(secondViewController, animated: true, completion: nil)
Change the storyboard name, view controller, and id as needed.
根据需要更改故事板名称,视图控制器和ID。
See also how to dismiss a view controller programmatically.
另请参见如何以编程方式关闭视图控制器。
#3
0
In Swift 3/4
在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)
#4
0
let storyB = UIStoryboard(name: "Main", bundle: nil)
let secondViewController =
storyB.instantiateViewController(withIdentifier:
"SecondViewControllerID") as! SecondViewController
self.present(secondViewController, animated: true, completion: nil)
In the secondViewController use this code to go back.
在secondViewController中使用此代码返回。
self.dismiss(animated: true, completion: nil)