2017-09-20 19 views
0

私はUserDefaultsが設定されているかどうかをチェックする機能を持っています。新しいView Controllerが開いていない場合、ユーザーのデフォルトを設定するログイン画面が表示されます。View Controllerをインスタンス化できません

私の問題は、ビューコントローラがインスタンス化していないですが、私は

func checkUserAccount() { 
     let defaults = UserDefaults.standard 
     let accountName = defaults.bool(forKey: "PUserAccountName") 
     let accountPassword = defaults.bool(forKey: "PUserAccountPassword") 
     if accountName == true && accountPassword == true { 
      print("User Registered") 

     } else { 

      let storyboard: UIStoryboard = UIStoryboard(name: "PolTRiM", bundle: nil) 
      let vc: StudentLoginVC = storyboard.instantiateViewController(withIdentifier: "studentLogin") as! StudentLoginVC 
      vc.modalPresentationStyle = .custom 
      vc.modalTransitionStyle = .crossDissolve 
      self.present(vc, animated: true, completion: { _ in }) 
      print("User not registered") 
     } 

    } 

任意の考えをプリント「登録されていないユーザー」を取得しますか?

+0

私はあなたのコードとその作業を試して、私はそこに何も見えないようにStudentLoginVCのビューのフレームに問題があると思う。 –

+0

アプリケーションがクラッシュしなかった場合は、強制的にインスタンスをキャストしているため、 'vc'がインスタンス化されていることを意味します。 @laxmankhanalは正しいです。 – Ryan

+0

@laxman、特定のエラーは見つかりませんでしたが、削除してVCをゼロから再構築し、それを上回るコードを使用することにしました。だから問題がないように見えても、元のVCやビューのどこかにいるはずです。 – Bowcaps

答えて

0

正しく書かれている場合、UIStoryBoardの名前とUIViewControllerのIDをダブルチェックしましたか?そうでなければ、このコードは私のために働いています

let storyboard = UIStoryboard(name: "Main", bundle: nil) 
let viewController = storyboard.instantiateViewController(withIdentifier :"MyViewController") as! UIViewController 
self.present(viewController, animated: true)  
+0

これは質問に対する答えを提供しません。十分な[評判](https://stackoverflow.com/help/whats-reputation)があれば、[投稿にコメントする]ことができます(https://stackoverflow.com/help/privileges/comment)。代わりに、[質問者からの明確化を必要としない回答を提供する](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-c​​an- i-do-代わりに)。 - [レビューから](レビュー/低品質の投稿/ 17395644) – Peter

+0

@bezoadam - はいすべてのスペルと構文が正しいです。 – Bowcaps

関連する問題