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

Ios 如何以编程方式以模态方式呈现视图控制器?,ios,objective-c,ios7,Ios,Objective C,Ios7,Edit:解决此问题时,我发现从UITabBarController开始,然后通过AppDelegate.m的didfishLaunchingwithoptions:方法执行登录验证要容易得多 问题: 此代码位于my AppDelegate.m中的应用程序didFinishLaunchingWithOptions:方法中 if([result isEqualToString: @"log"]) { UIStoryboard *storyboard = [UIStoryboard stor

Edit:解决此问题时,我发现从
UITabBarController
开始,然后通过
AppDelegate.m
didfishLaunchingwithoptions:
方法执行登录验证要容易得多

问题: 此代码位于my 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");
}
它只需为登录的用户获取一个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");
}
文档是一个很好的起点-文档将准确地告诉您需要知道的内容:

presentModalViewController:动画: 向用户显示由给定视图控制器管理的模式视图。(在iOS 6.0中已弃用。请改用。)

敏捷的 由于接受的答案是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 3/4中的。

     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)

在swift 4.2中,您可以这样做。对于那些希望在swift更新版本中获得此答案的人

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "ExampleViewController")
self.present(controller, animated: true, completion: nil)

您所需要做的就是查看此方法的参考文档,它将显示在其位置使用的内容。您不应该使用segue向后(而不是展开segue)返回到以前的控制器,这看起来就像您在本图中所做的一样。这将导致实例化新的控制器,而不是返回到您开始使用的控制器。系统知道您已接受问题的答案,并在相关时显示该信息。对不起,我不知道这些规则。正如你所看到的,我对这个城市很陌生。我希望在以下基础上删除这个问题:1。正在错误地处理问题2。我在提问之前没有搜索(请参阅答案下的第5条评论)好的,非常感谢,这帮助我理解了很多。我在使用其他逻辑开始登录页面时遇到了问题,这就是为什么我发布了图片如果你有不同的问题,你可以(也应该)问更多的问题。感谢您确认它的工作!哈哈,我不想再问另一个问题(我的反对票的b/c),但你是指这个问题还是其他问题?@WhiteHatPrince如果你有其他问题,你会问另一个问题。此外,为了避免否决票,请确保(1)在询问之前进行搜索,(2)使用简单的代码示例提供一个很好的解释。在这种情况下,我相信你没有先搜索。像这样的问题以前已经被问过并回答过了。没问题,很乐意帮忙!如果要从任意UI上下文执行此操作,可以使用UIWindow.visibleVC?.present
 self.dismiss(animated: true, completion: nil)
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "ExampleViewController")
self.present(controller, animated: true, completion: nil)