2017-11-30 20 views
-1

私は、アプリ用のシンプルなログインシステムの作成方法に関するBelal Khanのチュートリアルに従っていました。 私はチュートリアルの後、それは私がログアウトボタンがログアウトスウィフトでアプリがクラッシュする

ログインVCが存在するログインVCとProfileVCためのコードを下に掲載します をログアウトしたいポイントまで美しく動作します:

import UIKit 
import Alamofire 
class ViewController: UIViewController { 

//The login script url make sure to write the ip instead of localhost 
//you can get the ip using ifconfig command in terminal 
let URL_USER_LOGIN = "http://madrasati.site/userReg/v1/login.php" 

//the defaultvalues to store user data 
//the defaultvalues to store user data 
let defaultValues = UserDefaults.standard 

//the connected views 
//don't copy instead connect the views using assistant editor 
@IBOutlet weak var labelMessage: UILabel! 
@IBOutlet weak var textFieldUserName: UITextField! 
@IBOutlet weak var textFieldPassword: UITextField! 

//the button action function 
@IBAction func buttonLogin(_ sender: UIButton) { 

    //getting the username and password 
    let parameters: Parameters=[ 
     "username":textFieldUserName.text!, 
     "password":textFieldPassword.text! 
    ] 

    //making a post request 
    Alamofire.request(URL_USER_LOGIN, method: .post, parameters: parameters).responseJSON 
     { 
      response in 
      //printing response 
      print(response) 

      //getting the json value from the server 
      if let result = response.result.value { 
       let jsonData = result as! NSDictionary 

       //if there is no error 
       if(!(jsonData.value(forKey: "error") as! Bool)){ 

        //getting the user from response 
        let user = jsonData.value(forKey: "user") as! NSDictionary 

        //getting user values 
        let userId = user.value(forKey: "id") as! Int 
        let userName = user.value(forKey: "username") as! String 
        let userEmail = user.value(forKey: "email") as! String 
        let userPhone = user.value(forKey: "phone") as! String 

        //saving user values to defaults 
        self.defaultValues.set(userId, forKey: "userid") 
        self.defaultValues.set(userName, forKey: "username") 
        self.defaultValues.set(userEmail, forKey: "useremail") 
        self.defaultValues.set(userPhone, forKey: "userphone") 

        //switching the screen 
        let profileViewController = self.storyboard?.instantiateViewController(withIdentifier: "ProfileViewController") as! ProfileViewController 
        self.navigationController?.pushViewController(profileViewController, animated: true) 

        self.dismiss(animated: false, completion: nil) 
       }else{ 
        //error message in case of invalid credential 
        self.labelMessage.text = "Invalid username or password" 
       } 
      } 
    } 
} 

override func viewDidLoad() { 
    super.viewDidLoad() 
    //hiding the navigation button 
    let backButton = UIBarButtonItem(title: "", style: UIBarButtonItemStyle.plain, target: navigationController, action: nil) 
    navigationItem.leftBarButtonItem = backButton 

    // Do any additional setup after loading the view, typically from a nib. 

    //if user is already logged in switching to profile screen 
    if defaultValues.string(forKey: "username") != nil{ 
     let profileViewController = self.storyboard?.instantiateViewController(withIdentifier: "ProfileViewcontroller") as! ProfileViewController 
     self.navigationController?.pushViewController(profileViewController, animated: true) 

    } 
} 
} 

ProfileViewControllerは:

 
2017-11-30 22:03:33.987292+0400 registerationtest[40711:1152571] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Storyboard() doesn't contain a view controller with identifier 'ViewController'' 
*** First throw call stack: 
(
    0 CoreFoundation      0x0000000105e981ab __exceptionPreprocess + 171 
    1 libobjc.A.dylib      0x000000010552df41 objc_exception_throw + 48 
    2 UIKit        0x0000000106c2b1f3 -[UIStoryboard instantiateInitialViewController] + 0 
    3 registerationtest     0x0000000104a64221 _T017registerationtest21ProfileViewControllerC12buttonLogoutySo8UIButtonCF + 913 
    4 registerationtest     0x0000000104a6482c _T017registerationtest21ProfileViewControllerC12buttonLogoutySo8UIButtonCFTo + 60 
    5 UIKit        0x0000000106315275 -[UIApplication sendAction:to:from:forEvent:] + 83 
    6 UIKit        0x00000001064924a2 -[UIControl sendAction:to:forEvent:] + 67 
    7 UIKit        0x00000001064927bf -[UIControl _sendActionsForEvents:withEvent:] + 450 
    8 UIKit        0x00000001064916ec -[UIControl touchesEnded:withEvent:] + 618 
    9 UIKit        0x000000010638abbb -[UIWindow _sendTouchesForEvent:] + 2807 
    10 UIKit        0x000000010638c2de -[UIWindow sendEvent:] + 4124 
    11 UIKit        0x000000010632fe36 -[UIApplication sendEvent:] + 352 
    12 UIKit        0x0000000106c72434 __dispatchPreprocessedEventFromEventQueue + 2809 
    13 UIKit        0x0000000106c75089 __handleEventQueueInternal + 5957 
    14 CoreFoundation      0x0000000105e3b231 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17 
    15 CoreFoundation      0x0000000105edae41 __CFRunLoopDoSource0 + 81 
    16 CoreFoundation      0x0000000105e1fb49 __CFRunLoopDoSources0 + 185 
    17 CoreFoundation      0x0000000105e1f12f __CFRunLoopRun + 1279 
    18 CoreFoundation      0x0000000105e1e9b9 CFRunLoopRunSpecific + 409 
    19 GraphicsServices     0x000000010e1809c6 GSEventRunModal + 62 
    20 UIKit        0x00000001063135e8 UIApplicationMain + 159 
    21 registerationtest     0x0000000104a63827 main + 55 
    22 libdyld.dylib      0x000000010a3bed81 start + 1 
    23 ???         0x0000000000000001 0x0 + 1 
) 
libc++abi.dylib: terminating with uncaught exception of type NSException 
(lldb) 

import UIKit 

class ProfileViewController: UIViewController { 

    //label again don't copy instead connect 
    @IBOutlet weak var labelUserName: UILabel! 

    //button 
    @IBAction func buttonLogout(_ sender: UIButton) { 

     //removing values from default 
     UserDefaults.standard.removePersistentDomain(forName: Bundle.main.bundleIdentifier!) 
     UserDefaults.standard.synchronize() 

     //switching to login screen 
     let loginViewController = self.storyboard?.instantiateViewController(withIdentifier: "ViewController") as! ViewController 
     self.navigationController?.pushViewController(loginViewController, animated: true) 
     self.dismiss(animated: false, completion: nil) 
    } 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     //hiding back button 
     let backButton = UIBarButtonItem(title: "", style: UIBarButtonItemStyle.plain, target: navigationController, action: nil) 
     navigationItem.leftBarButtonItem = backButton 

     //getting user data from defaults 
     let defaultValues = UserDefaults.standard 
     if let name = defaultValues.string(forKey: "username"){ 
      //setting the name to label 
      labelUserName.text = name 
     }else{ 
      //send back to login view controller 
     } 

    } 
} 

これは私が取得していますエラーです

EDIT: ストーリーボードのIDを割り当てた後、私はまだエラーを取得しています:

 
2017-11-30 22:05:22.424198+0400 registerationtest[40745:1154215] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Storyboard() doesn't contain a view controller with identifier 'ViewController'' 
*** First throw call stack: 
(
    0 CoreFoundation      0x000000010cb6d1ab __exceptionPreprocess + 171 
    1 libobjc.A.dylib      0x000000010c202f41 objc_exception_throw + 48 
    2 UIKit        0x000000010d9001f3 -[UIStoryboard instantiateInitialViewController] + 0 
    3 registerationtest     0x000000010b739221 _T017registerationtest21ProfileViewControllerC12buttonLogoutySo8UIButtonCF + 913 
    4 registerationtest     0x000000010b73982c _T017registerationtest21ProfileViewControllerC12buttonLogoutySo8UIButtonCFTo + 60 
    5 UIKit        0x000000010cfea275 -[UIApplication sendAction:to:from:forEvent:] + 83 
    6 UIKit        0x000000010d1674a2 -[UIControl sendAction:to:forEvent:] + 67 
    7 UIKit        0x000000010d1677bf -[UIControl _sendActionsForEvents:withEvent:] + 450 
    8 UIKit        0x000000010d1666ec -[UIControl touchesEnded:withEvent:] + 618 
    9 UIKit        0x000000010d05fbbb -[UIWindow _sendTouchesForEvent:] + 2807 
    10 UIKit        0x000000010d0612de -[UIWindow sendEvent:] + 4124 
    11 UIKit        0x000000010d004e36 -[UIApplication sendEvent:] + 352 
    12 UIKit        0x000000010d947434 __dispatchPreprocessedEventFromEventQueue + 2809 
    13 UIKit        0x000000010d94a089 __handleEventQueueInternal + 5957 
    14 CoreFoundation      0x000000010cb10231 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17 
    15 CoreFoundation      0x000000010cbafe41 __CFRunLoopDoSource0 + 81 
    16 CoreFoundation      0x000000010caf4b49 __CFRunLoopDoSources0 + 185 
    17 CoreFoundation      0x000000010caf412f __CFRunLoopRun + 1279 
    18 CoreFoundation      0x000000010caf39b9 CFRunLoopRunSpecific + 409 
    19 GraphicsServices     0x0000000114dca9c6 GSEventRunModal + 62 
    20 UIKit        0x000000010cfe85e8 UIApplicationMain + 159 
    21 registerationtest     0x000000010b738827 main + 55 
    22 libdyld.dylib      0x0000000111001d81 start + 1 
    23 ???         0x0000000000000001 0x0 + 1 
) 
libc++abi.dylib: terminating with uncaught exception of type NSException 
(lldb) 

答えて

0

エラーは、この行を参照している:

let loginViewController = self.storyboard?.instantiateViewController(withIdentifier: "ViewController") as! ViewController 

それはあなたの避難所」ことを語っていますストーリーボード内の任意のUIViewControllerに識別子 "ViewController"を割り当てました。ストーリーボードに移動し、UIViewControllerをクリックし、右側のパネルでIDインスペクタをクリックし、Storyboard IDフィールドに "ViewController"を入力します。

enter image description here

+0

私はそれに気付かなかったが、同じストーリーボードで2 viewControllersがあります今、私はそれがID与えてくれた、それはまだ私にエラー – SwiftyDude

+0

を与えていますか? – creeperspeak

+0

nvmとても感謝しています。問題は私のXcodeが盗まれていて、それが正しく構築されていないということでした。どうもありがとう! – SwiftyDude

関連する問題