如何以编程方式以模态方式呈现视图控制器?

Chi*_*isx 16 objective-c ios ios7

编辑:当解决这个问题时,我发现你的方法开始UITabBarController,然后通过AppDelegate.mdidFinishLaunchingWithOptions:方法执行登录验证要容易得多.

问题: 此代码application didFinishLaunchingWithOptions:位于AppDelegate.m中的方法中

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");
}
Run Code Online (Sandbox Code Playgroud)

它只是为登录的用户提供HTTP响应,并将它们带到我的TabBarController.问题是它使用推送而不是模态转换来显示页面.由于在iOS7中不推荐使用presentModalViewController方法,因此如何以编程方式强制进行模式演示?

编辑: )

Und*_*ndo 30

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");
}
Run Code Online (Sandbox Code Playgroud)

文档是一个很好的起点 - 文档UIViewController presentViewController:animated可以准确地告诉您需要知道的内容:

presentModalViewController:动画:

呈现由给定视图控制器管理给用户的模态视图.(在iOS 6.0中不推荐使用.请presentViewController:animated:completion:改用.)

- (void)presentModalViewController:(UIViewController *)modalViewController animated:(BOOL)animated
Run Code Online (Sandbox Code Playgroud)

  • @WhiteHatPrince如果你有另一个问题,你会问另一个问题.此外,为了避免downvotes,请确保您(1)在询问之前进行搜索,并且(2)使用简单的代码示例包含一个很好的解释.在这种情况下,我相信你没有先搜索.之前已经提出并回答过很多这样的问题. (3认同)

Sur*_*gch 21

迅速

由于接受的答案是在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)
Run Code Online (Sandbox Code Playgroud)

根据需要更改故事板名称,视图控制器和ID.

另请参见如何以编程方式关闭视图控制器.