2016-07-09 65 views
2

我有一個rails api設置和一個測試用戶。我可以登錄/註銷查看api。Swift:從RESTful API驗證後更改ViewController

我有一個選項卡應用程序設置,我有一個LoginViewController設置爲進行身份驗證。我的視圖控制器(基本上)看起來像下面的代碼。這不工作。然而,代碼第二塊確實工作

我猜我打電話一些錯誤。第一執行塊,然後與***斷言失敗崩潰 - [UIKeyboardTaskQueuewaitUntilAllTask​​sAreFinished] ..我一直我的車輪旋轉了幾個小時..試圖看着塞格...等等任何東西都會有所幫助!

//DOES NOT WORK // 

class LoginViewController: UIViewController { 

@IBOutlet weak var email: UITextField! 

@IBOutlet weak var password: UITextField! 

@IBAction func Login(sender: AnyObject) { 
    func switchToTabs() { 
     let storyboard = UIStoryboard(name: "Main", bundle: nil) 
     let tabBarController = storyboard.instantiateViewControllerWithIdentifier("TabBarController") as! UITabBarController 
     let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate 
     appDelegate.window?.rootViewController = tabBarController 

    } 

    let authCall = PostData() 
    var authToken = "" 
    authCall.email = email.text! 
    authCall.password = password.text! 
    authCall.forData { jsonString in 
     authToken = jsonString 
     if(authToken != "") { 
      switchToTabs() 
     } 
    } 
} 



// SAME CLASS THAT DOES WORK// 

class LoginViewController: UIViewController { 

@IBOutlet weak var email: UITextField! 

@IBOutlet weak var password: UITextField! 

@IBAction func Login(sender: AnyObject) { 
     let storyboard = UIStoryboard(name: "Main", bundle: nil) 
     let tabBarController = storyboard.instantiateViewControllerWithIdentifier("TabBarController") as! UITabBarController 
     let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate 
     appDelegate.window?.rootViewController = tabBarController 


} 

我的問題是,爲什麼我似乎可以導航到一個新的觀點,當我不使用我的API,但是當我做我收到的身份驗證令牌。我無法登錄。

請原諒醜陋的代碼..這是我第一天的快速

回答

2

每當你做任何事情改變了關於內部UI的內容或任何甚至可能離開主隊列的東西時,都要將它放在這個語句中。

dispatch_async(dispatch_get_main_queue()) { 
    //the code that handles UI 
} 

這包括像一個封閉的內部塞格斯的所有代碼,你有

authCall.forData { jsonString in 
    authToken = jsonString 
    if(authToken != "") { 
     switchToTabs() 
    } 
} 

如果你把它寫像這樣它會工作

authCall.forData { 
    jsonString in 
    authToken = jsonString 
    if(authToken != "") { 
     dispatch_async(dispatch_get_main_queue()) { 
      switchToTabs() 
     } 
    } 
} 

然後打電話給你的SEGUE在switchToTabs()

如果你不確定你在排隊,它不會傷害到d它。隨着您更熟悉將您帶離主隊列的場景,您將意識到何時使用它。無論何時您處於關閉狀態並處理UI都是您可以離開的安全賭注。顯然任何異步都會導致它。只需將你的performSegueWithIdentifier放在裏面,它就可以工作。

+0

你。是。的。人。謝謝! – MattEm

0

如果你在視圖控制器attatched到LoginViewController故事板上,然後你點擊segue你可以給它一個標識符。然後你只需調用performSegueWithIdentifier(「identifierName」,sender:nil),switchToTabs()中的其他代碼都不是必需的。

如果你想繼續沿着你正在採取的路線召喚視圖控制器,那麼我認爲你缺少的是這些線。

在你的班級以外的地方使用它。

private extension UIStoryboard { 
    class func mainStoryboard() -> UIStoryboard { return UIStoryboard(name: "Main", bundle: NSBundle.mainBundle()) } 

    class func tabBarController() -> UITabBarController? { 
     return mainStoryboard().instantiateViewControllerWithIdentifier("TabBarController") as? UITabBarController 
    } 
} 

然後改變你的switchToTabs()函數從該

func switchToTabs() { 
    let tabBarController = UIStoryboard.tabBarController() 
    view.addSubview(tabBarController!.view) 

    //line below can be altered for different frames 
    //tabBarController!.frame = self.frame 

    addChildViewController(tabBarController!) 
    tabBarController!.didMoveToParentViewController(self) 

    //line below is another thing worth looking into but doesn't really correlate with this 
    //self.navigationController!.pushViewController(tabBarController!, animated: true) 
} 

我在一對夫婦的意見添加爲你考慮,如果你喜歡。我相信這可以完成這項工作,但請記住,您所走的方向並不會從內存中移除前一個屏幕。它在技術上仍然坐在你的新屏幕後面。我不會爲你想要做的事推薦這個,但如果你正在構建一些簡單的東西,它可能不會受到傷害。

+0

所以我從AppDelegate以同樣的方式調用登錄控制器..有沒有一種方法可以首先進入loginVC?或者我應該繼續調用LoginVC,只是改變這個segue。非常感謝您的幫助btw .. – MattEm

+0

似乎preformSegueWithIdentifier仍然以相同的方式失敗...>< – MattEm

+0

self.navigationController!.pushViewController(tabBarController!,animated:true)行是這樣做的編程方式我相信沒有賽段,但我從來沒有使用過這種方法。確保你的完成關閉實際上被調用。 [apple segue文檔](https://developer.apple.com/library/ios/recipes/xcode_help-IB_storyboard/Chapters/StoryboardSegue.html)segue必須來自與調用它的類關聯的ViewController,並且標識符String必須我完全相同。一個大寫字母的區別,它不會工作。 – Sethmr