2016-09-04 8 views
3

私はSwift(そして一般的なプログラミング)を使い慣れています。私はログイン機能でXcodeにいくつかの困難を抱えています。私が望むのは、エラーが返されず、ユーザーが別のコントローラーに送られなければならない場合、ユーザーはログインする必要があるということです。私はAppleからのドキュメントとperformSegueWithIdentifier(もちろんいくつかの質問はここで尋ねられました)を読んだことがありますが、識別子が与えられたpushまたはmodalのセグを使用してもまだ動作しません。アプリがクラッシュするか、資格情報が間違っていてもUITabBarControllerにユーザーが送られます。ViewController segueとlogin

これは私のLogInViewController.swiftです:

@IBAction func loginAction(sender: AnyObject) 
{ 
    if self.emailField.text == "" || self.passwordField.text == "" 
    { 
     let alertController = UIAlertController(title: "Oops!", message: "Please enter an email and password.", preferredStyle: .Alert) 

     let defaultAction = UIAlertAction(title: "OK", style: .Cancel, handler: nil) 
     alertController.addAction(defaultAction) 

     self.presentViewController(alertController, animated: true, completion: nil) 
    } 
    else 
    { 
     FIRAuth.auth()?.signInWithEmail(self.emailField.text!, password: self.passwordField.text!) { (user, error) in 

      if error == nil 
      { 
       self.emailField.text = "" 
       self.passwordField.text = "" 
      } 
      else 
      { 
       let alertController = UIAlertController(title: "Oops!", message: error?.localizedDescription, preferredStyle: .Alert) 

       let defaultAction = UIAlertAction(title: "OK", style: .Cancel, handler: nil) 
       alertController.addAction(defaultAction) 

       self.presentViewController(alertController, animated: true, completion: nil) 
      } 
      // User Logged in 
       self.performSegueWithIdentifier("LoggedIn", sender: self) 
     } 
    } 
} 

私はコンソールで取得エラー:

2016年9月4日14:55:30.019 DrinkApp [37777:1006336] ** *未知の例外 'NSGenericException'のためアプリを終了します、理由: 'Segue' LoggedIn 'のナビゲーションコントローラを見つけることができませんでした。プッシュセグは、ソースコントローラがUINavigationControllerのインスタンスによって管理されている場合にのみ使用できます。 'ここで

答えて

-1

Firebaseログインの例である:

func login() { 
     FIRAuth.auth()?.signInWithEmail(textFieldLoginEmail.text!, password: textFieldLoginPassword.text!) { (user, error) in 
      if error != nil { 
       let alert = UIAlertController(title: "User Authentication error", 
               message: error?.localizedDescription, 
               preferredStyle: .Alert) 
       let OKAction = UIAlertAction(title: "OK", 
              style: .Default) { (action: UIAlertAction!) -> Void in 
       } 
       let resetPasswordAction = UIAlertAction(title: "OK", 
                 style: .Default) { (action: UIAlertAction!) -> Void in 
       } 
       alert.addAction(OKAction) 
       guard let errorCode = FIRAuthErrorCode(rawValue: (error?.code)!) else { return } 
       switch errorCode { 
       case .ErrorCodeWrongPassword: 
        alert.addAction(resetPasswordAction) 
       case .ErrorCodeNetworkError: 
        print("Network error") 
       default: 
        print("Other error\n") 
        break 
       } 
       self.presentViewController(alert, animated: true, completion: nil) 
      } else { // User Logged in 
       self.performSegueWithIdentifier("MySegue", sender: self) 
      } 
     } 
    } 

私はこの例では2つのエラーを処理していました。

push segueを使用する場合は、ナビゲーションコントローラが必要です。root seguelogin view controllerに、次にsegueは次のビューコントローラになります。あなたの問題はFirebaseではありませんが、ストーリーボードを設定しているようですので :この最後のsegueは(この場合には、それはMySegueです)

編集を識別子を持っている必要があります。次に例を示します。

真ん中UIViewControllersUINavigationControllerとのUIViewControllerroot view controller
  • 、ポップアップメニューから[コントロール
    ドラッグを選ぶとの間の制御ドラッグ1
  • を設定
    1. 最後にUIViewControllersを選択してshow
    2. を選択してください。 (属性インスペクタ内の)識別子をMySegueに変更します。 5. Middle UIViewControllersを選択し、(アイデンティティインスペクタの)クラス名をログインビューコントローラクラスに変更します。ここ

    完全なセットアップである: full setup

    制御ドラッグメニュー(ステップ2):

    step 2

    セグエインスペクタ(ステップ4)属性:

    step4

  • +0

    ありがとうございますが、私はまだそれを取得しません。 self.presentViewController:私はperformSegueWithIdentifier追加しました(alertController、アニメーション:真、完了を:ゼロ) } ユーザーは self.performSegueWithIdentifier( "LOGGEDIN"、送信者:自己)にログイン// しかし、私は、これを取得しますコンソール: 2016-09-04 14:20:56。968 DrinkApp [35936:985848] ***キャッチされていない例外 'NSGenericException'のためアプリを終了しています、理由: 'Segue' LoggedIn 'のナビゲーションコントローラが見つかりませんでした。プッシュセグは、ソースコントローラがUINavigationControllerのインスタンスによって管理されている場合にのみ使用できます。 ' – Joesoes

    3

    ユーザーを別のView Controllerに送る主な方法。最初はperformSegueWithIdentifierです。これを使用する方法は次のとおりです。

    まず初めに、元のVCの黄色の円からインターフェースビルダーのターゲットVCにドラッグします。

    The Yellow Circle

    次、2つのビューコントローラの間に表示される「セグエ矢印」をクリックします。 mySegue識別子フィールド、タイプに

    the arrow

    here

    次に、あなたのコードに戻ります。あなたはセクションの看板に到達すると、次のコードを追加します。

    self.performSegueWithIdentifier("mySegue", sender: nil) 
    

    第2の方法は、もう少し進んでいます。その方法は次のとおりです。

    まず、コピー先VCの黄色い丸をクリックします。新聞をクリックして、ストーリーボードIDmyVCに電話してください。

    バックコードの同じ部分にストーリーボードID

    here

    移動をクリックして、このコードをコピー&ペーストすることを確認します:ダウン以下

    let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil) 
        let initViewController: UIViewController = storyboard.instantiateViewControllerWithIdentifier("myVC") as UIViewController 
        self.presentViewController(initViewController, animated: true, completion: nil) 
    

    コメントご質問がある場合は、がんばろう!

    関連する問題