2016-07-09 7 views
2

私は、レールapiと1人のテストユーザーを設定しています。私はログインすることができます/ログアウトは、APIを表示します。スイフト:RESTful APIからの認証後にViewControllerを変更してください

私はタブアプリケーションをセットアップしており、LoginViewControllerがそれを認証するように設定されています。私のビューコントローラ(基本的に)は、以下のコードのように見えます。これはdoesnt workです。しかし、コードの第2のブロックは

私は私が何か間違ったことを呼び出しています推測してい

...最初のブロックの実行を働き、その後に***アサーション失敗でクラッシュしない - [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 != "") { 
     dispatch_async(dispatch_get_main_queue()) { 
      switchToTabs() 
     } 
    } 
} 

を動作しますので、あなたのようにそれを書けば

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

が、その後switchToTabs()であなたのセグエを呼び出していたような閉鎖の内側seguesすべてのコードが含まれています

あなたがどのキューにいるかわからない場合、それはdに害を与えませんそれ。メインキューから外れるシナリオに慣れると、いつ使用するのかが分かります。あなたが閉じることができ、UIを扱うときはいつでも、あなたが残すことができる安全な賭けです。明らかに非同期のものであればそれも同様です。あなたのperformSegueWithIdentifierをその中に置くだけでうまくいくはずです。

+0

あなた。あります。 。おとこ。ありがとう! – MattEm

0

LoginViewControllerにアタッチされたView ControllerからSegueを作成してSegueをクリックすると、そのSegueに識別子を付けることができます。 performSegueWithIdentifier( "identifierName"、sender:nil)を呼び出すだけで、switchToTabs()の他のコードは必要ありません。

あなたがView Controllerを呼び出すために行っているルートを続行したい場合は、あなたが見逃しているのはこれらの行だと思います。

これをあなたのクラスの外で使用してください。その後

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

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

この

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) 
} 

のためにあなたのswitchToTabs()関数を変更する私はあなたが好きならに見てあなたのためのカップルのコメントで追加。私はこれが仕事をするだろうと信じていますが、あなたが向かう方向が前の画面をメモリから削除しないことに留意してください。それは技術的にはまだあなたの新しい画面の後ろに座っています。私はあなたがしようとしていることについてこれを推薦することはありませんが、単純なものを構築しているならば傷つけないかもしれません。

+0

私はAppDelegateから同じようにログインコントローラを呼び出しています。まずはloginVCを常に最初に使う方法がありますか?または私はLoginVCを同じように呼び出して、このセグをちょうど変更する必要があります。あなたの助けてくれてありがとうございました.. – MattEm

+0

preformSegueWithIdentifierはまだ同じように失敗します...>。< – MattEm

+0

self.navigationController!.pushViewController(tabBarController!、animated:true)行は、私が信じるプログラム的な方法です私はその方法を使ったことはありませんでした。完了クロージャが実際に呼び出されていることを確認してください。 [apple segue documentation](https://developer.apple.com/library/ios/recipes/xcode_help-IB_storyboard/Chapters/StoryboardSegue.html)Segueはそれを呼び出すクラスに関連付けられたViewControllerから来なければならず、識別子Stringは私は同じです。 1つの大文字の違いとそれは動作しません。 – Sethmr

関連する問題