【问题标题】:App crashes on Logout Swift [duplicate]应用程序在 Logout Swift 上崩溃 [重复]
【发布时间】:2018-05-14 18:03:30
【问题描述】:

所以我在关注 Belal Khan 关于如何为我的应用制作简单登录系统的教程。 我按照教程进行操作,它运行良好,直到我想注销为止 我将发布登录 VC 和存在注销按钮的 ProfileVC 的代码

登录VC:

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:

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
        }

    }
 }

这是我得到的错误:

2017-11-30 22:03:33.987292+0400 registerationtest[40711:1152571] *** 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“Storyboard () 不包含标识符为“ViewController”的视图控制器 *** 首先抛出调用堆栈: ( 0 核心基础 0x0000000105e981ab __exceptionPreprocess + 171 1 libobjc.A.dylib 0x000000010552df41 objc_exception_throw + 48 2 UIKit 0x0000000106c2b1f3-[UIStoryboard instantiateInitialViewController] + 0 3 注册测试 0x0000000104a64221 _T017registerationtest21ProfileViewControllerC12buttonLogoutySo8UIButtonCF + 913 4 注册测试 0x0000000104a6482c _T017注册测试21ProfileViewControllerC12buttonLogoutySo8UIButtonCFTo + 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 发送事件:] + 4124 11 UIKit 0x000000010632fe36-[UIApplication sendEvent:] + 352 12 UIKit 0x0000000106c72434 __dispatchPreprocessedEventFromEventQueue + 2809 13 UIKit 0x0000000106c75089 __handleEventQueueInternal + 5957 14 核心基础 0x0000000105e3b231 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17 15 核心基础 0x0000000105edae41 __CFRunLoopDoSource0 + 81 16 核心基础 0x0000000105e1fb49 __CFRunLoopDoSources0 + 185 17 核心基础 0x0000000105e1f12f __CFRunLoopRun + 1279 18 核心基础 0x0000000105e1e9b9 CFRunLoopRunSpecific + 409 19 图形服务 0x000000010e1809c6 GSEventRunModal + 62 20 UIKit 0x00000001063135e8 UIApplicationMain + 159 21 注册测试 0x0000000104a63827 主要 + 55 22 libdyld.dylib 0x000000010a3bed81 开始 + 1 23 ??? 0x0000000000000001 0x0 + 1 ) libc++abi.dylib:以 NSException 类型的未捕获异常终止 (lldb)

编辑: 分配情节提要 ID 后,我仍然收到错误消息:

2017-11-30 22:05:22.424198+0400 registerationtest[40745:1154215] *** 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“Storyboard () 不包含标识符为“ViewController”的视图控制器 *** 首先抛出调用堆栈: ( 0 核心基础 0x000000010cb6d1ab __exceptionPreprocess + 171 1 libobjc.A.dylib 0x000000010c202f41 objc_exception_throw + 48 2 UIKit 0x000000010d9001f3-[UIStoryboard 实例化初始视图控制器] + 0 3 注册测试 0x000000010b739221 _T017注册测试21ProfileViewControllerC12buttonLogoutySo8UIButtonCF + 913 4 注册测试 0x000000010b73982c _T017注册测试21ProfileViewControllerC12buttonLogoutySo8UIButtonCFTo + 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 发送事件:] + 4124 11 UIKit 0x000000010d004e36-[UIApplication sendEvent:] + 352 12 UIKit 0x000000010d947434 __dispatchPreprocessedEventFromEventQueue + 2809 13 UIKit 0x000000010d94a089 __handleEventQueueInternal + 5957 14 核心基础 0x000000010cb10231 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17 15 核心基础 0x000000010cbafe41 __CFRunLoopDoSource0 + 81 16 核心基础 0x000000010caf4b49 __CFRunLoopDoSources0 + 185 17 核心基础 0x000000010caf412f __CFRunLoopRun + 1279 18 核心基础 0x000000010caf39b9 CFRunLoopRunSpecific + 409 19 图形服务 0x0000000114dca9c6 GSEventRunModal + 62 20 UIKit 0x000000010cfe85e8 UIApplicationMain + 159 21 注册测试 0x000000010b738827 主要 + 55 22 libdyld.dylib 0x0000000111001d81 开始 + 1 23 ??? 0x0000000000000001 0x0 + 1 ) libc++abi.dylib:以 NSException 类型的未捕获异常终止 (lldb)

【问题讨论】:

    标签: ios swift storyboard


    【解决方案1】:

    错误是指这一行:

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

    它告诉您,您尚未将标识符“ViewController”分配给情节提要中的任何 UIViewController。进入你的storyboard,点击UIViewController,点击右侧面板中的Identity Inspector,在Storyboard ID字段中填写“ViewController”。

    【讨论】:

    • 我没有注意到,但现在我给了它一个 ID,它仍然给我一个错误
    • 这两个 viewController 是否在同一个故事板中?
    • nvm 现在可以正常工作了,非常感谢。问题是我的 Xcode 被窃听并且没有正确构建,并非一切都很好。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 2018-03-16
    • 1970-01-01
    • 2018-08-03
    • 1970-01-01
    • 2023-03-21
    • 2015-12-22
    • 1970-01-01
    • 2017-10-13
    相关资源
    最近更新 更多