前提・実現したいこと
初回起動時のみ表示したいviewがあります。
初回かどうかの判断はできているのですが、表示しようとするとエラーが出ます。
初回起動時以外はエラーもなく表示できています。
debugエリアで以下のエラーがでています。
発生している問題・エラーメッセージ
Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<sampleCoreBluetooth.ViewController 0x7fcd925098c0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key ownNumber.'
該当のソースコード
Swift
1 2@UIApplicationMain 3class AppDelegate: UIResponder, UIApplicationDelegate { 4 var window: UIWindow? 5 6 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 7 8 let ud = UserDefaults.standard 9 let dict = ["firstLaunch": true] 10 ud.register(defaults: dict) 11 12 if ud.bool(forKey: "firstLaunch"){ 13 ud.set(false, forKey: "firstLaunch") 14 print("初回起動") 15 16 let viewControllers = ViewController(nibName: "StartViewController", bundle: nil) 17 window = UIWindow(frame: UIScreen.main.bounds) 18 window?.rootViewController = viewControllers 19 window?.makeKeyAndVisible() 20 } 21 print("初回起動ではない") 22 23 //tabbar 配列 24 var viewControllers: [UIViewController] = [] 25 26 //1ページ目 27 let firstTab = AttendanceView() 28 firstTab.tabBarItem = UITabBarItem(title: "attend", image: UIImage(named: "dash"), tag: 1) 29 viewControllers.append(firstTab) 30 31 //2ページ目 32 let secondTab = ChangeAttend() 33 secondTab.tabBarItem = UITabBarItem(title: "status", image: UIImage(named: "pin"), tag: 2) 34 viewControllers.append(secondTab) 35 36 //セット 37 let tabBarController = UITabBarController() 38 tabBarController.setViewControllers(viewControllers, animated: false) 39 40 let colorKey = UIColor(red: 249/155, green: 161/255, blue: 188/255, alpha: 1.0) 41 UITabBar.appearance().tintColor = colorKey 42 43 window = UIWindow(frame: UIScreen.main.bounds) 44 window?.rootViewController = tabBarController 45 window?.makeKeyAndVisible() 46 47 return true 48 } 49 50
試したこと
初回起動時に表示したいviewにownNumberでtextFieldを置いてます。
@IBOutletも作り直し、名前をかえても同じエラーが表示されます。。
解決方法
初回のみ登録してほしい番号があったので、それをuserDefaultsに登録。
AttendanceView
1 override func viewDidAppear(_ animated: Bool) { 2 if ud.object(forKey: "Number") == nil { 3 print("ud ない") 4 let vc = StartViewController() 5 self.present(vc, animated: true, completion: nil) 6 7 } else { 8 print("ud ある") 9 } 10 }
これで、初回起動&userDefaultsに値がない場合StartViewControllerに飛んでくれました。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/07/29 02:12
2018/07/29 02:34
2018/07/31 08:26