在执行了一些网络代码后(使用 AlamoFire,在闭包内),我试图转到另一个 ViewController(它是 TabBarController 的一部分)。我现在的 VC 的名字是 StartingVC。在我的闭包验证了一些代码之后,我想去另一个 VC。在我的示例中,代码得到满足,我 100% 确定,因为我有一个指示如此的 println()
,但它不会将我带到另一个 VC。
我的关闭代码如下:
Alamofire.request(.POST, "http://localhost/test.php", parameters: dataToSend).responseJSON{
(request, response, data, error) in
self.showViewController(HomeViewController(), sender: nil)
我知道我需要在闭包中使用 self
来引用我正在使用的实际 VC。但是代码不起作用。
你知道如何使用代码呈现/推送/转到另一个 VC 吗?我没有使用任何 Storyboard链接到我的 HomeViewController,因为在以编程方式和在 Storyboard中使用 segue 之前我遇到了一些问题,这就是我决定采用代码方法的原因。
提前谢谢你的帮助
干杯!
最佳答案
如果您不想使用 Storyboard设置导航 Controller 并且无法从那里开始,您的 appdelegate 应该是这样的:
class AppDelegate: UIResponder, UIApplicationDelegate {
//you must mark the window and navigationController variables as optional
var window: UIWindow?
var navigationController: UINavigationController?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
navigationController = UINavigationController()
var homeViewController: UIViewController = UIViewController()
self.navigationController.pushViewController(homeViewController, animated: false)
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
self.window.rootViewController = navigationController
self.window.backgroundColor = UIColor.whiteColor()
self.window.makeKeyAndVisible()
return true
}
}
除了在 View Controller 上,您还可以将带有 Storyboard对象的 View Controller 推送为:
var firstViewController = self.storyboard!.instantiateViewControllerWithIdentifier("firstViewController") as! firstViewController
self.navigationController?.pushViewController(firstViewController, animated: false);
当前 View Controller :
let vc = ViewController() //change this to your class name
self.presentViewController(vc, animated: true, completion: nil)
关于ios - 在关闭中转到另一个 ViewController 不能快速工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30796459/