2015-06-12 79 views
1

我想在執行一些網絡代碼(使用AlamoFire,在一個閉包內)後去另一個ViewController(這是TabBarController的一部分)。我當前VC的名字是StartingVC。我想在我的關閉驗證了一些代碼後再去另一個VC。在我的例子中,代碼得到滿足,我100%確定,因爲我有一個println()表明如此,但它不會把我帶到另一個VC。去封閉中的另一個ViewController不能在swift中工作

我關閉代碼如下:

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,因爲在編程和故事板中使用segue之前我遇到了一些問題,這就是爲什麼我決定採用代碼方法。

感謝您提前幫忙

乾杯!

回答

0

如果你不想設置導航控制器與故事板和無從下手那裏,你的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 
} 
} 

比視圖控制器上,你可以把視圖控制器與分鏡對象作爲其他:

var firstViewController = self.storyboard!.instantiateViewControllerWithIdentifier("firstViewController") as! firstViewController 
self.navigationController?.pushViewController(firstViewController, animated: false); 

當前視圖控制器:

let vc = ViewController() //change this to your class name 
self.presentViewController(vc, animated: true, completion: nil) 
+0

傑克,就是幫助你嗎? – Arvind

相關問題