简体   繁体   中英

Swift Project Terminating with uncaught exception of type NSException

Unrecognized selector sent to instance 0x7feca9469620 2016-05-10 19:34:58.781 TribeA2[76123:4834825] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[TribeA2.RegisterPageViewController registerButtonTapped:]: unrecognized selector sent to instance 0x7feca9469620'

Terminating with uncaught exception of type NSException .

import UIKit

class RegisterPageViewController: UIViewController {



@IBOutlet weak var userFirstNameTextField: UITextField!
@IBOutlet weak var userLastNameTextField: UITextField!
@IBOutlet weak var userEmailTextField: UITextField!
@IBOutlet weak var userPasswordTextField: UITextField!


var databasePath = NSString()

override func viewDidLoad() {
    super.viewDidLoad()
    let filemgr = NSFileManager.defaultManager()
    let dirPaths =
        NSSearchPathForDirectoriesInDomains(.DocumentDirectory,
                                            .UserDomainMask, true)

    let docsDir = dirPaths[0]

    databasePath = (docsDir as NSString).stringByAppendingPathComponent(
        "users.db")

    if !filemgr.fileExistsAtPath(databasePath as String) {

        let userDB = PersonDatabase(path: databasePath as String)

        if userDB == nil {
            print("Error: \(userDB.lastErrorMessage())")
        }

        if userDB.open() {
            let sql_stmt = "CREATE TABLE IF NOT EXISTS USERS (ID INTEGER PRIMARY KEY AUTOINCREMENT, FNAME TEXT, LNAME TEXT, EMAIL TEXT, PASSWORD TEXT)"
            if !userDB.executeStatements(sql_stmt) {
                print("Error: \(userDB.lastErrorMessage())")
            }
            userDB.close()
        } else {
            print("Error: \(userDB.lastErrorMessage())")
        }
    }


}


@IBAction func saveData(sender: AnyObject) {
    let userDB = PersonDatabase(path: databasePath as String)

    if userDB.open() {

        func displayMyAlertMessage(userMessage:String)
        {
            let myAlert = UIAlertController(title:"Alert", message:
                userMessage, preferredStyle:
                UIAlertControllerStyle.Alert);

            let okAction = UIAlertAction(title:"Ok", style:
                UIAlertActionStyle.Default, handler:nil)

            myAlert.addAction(okAction)

            self.presentViewController(myAlert, animated:true,
                                       completion:nil)

        }


        let insertSQL = "INSERT INTO USERS (fname, lname, email, password) VALUES ('\(userFirstNameTextField.text)', '\(userLastNameTextField.text)' '\(userEmailTextField.text)', '\(userPasswordTextField.text)')"

        let result = userDB.executeUpdate(insertSQL, withArgumentsInArray: nil)

        if !result {
            displayMyAlertMessage("All fields are required")
            print("Error: \(userDB.lastErrorMessage())")
            return
        } else {
           displayMyAlertMessage("Thank you for registering \(userFirstNameTextField.text)")
    }
}

in RegisterPageViewController you were created the registerButtonTapped button action, but button method not implemented, if you are not used the registerButtonTapped delete from attribute inspector, else implenent the button action on class like the following

func registerButtonTapped(sender: UIButton)
    {
   }

In the interface builder if you right-click on that button, a popup will be shown where you can see that you have "action" connected with name -registerButtonTapped: to your class, but actually you haven't implemented that method (or you deleted it for some reason). So you must delete that connection by clicking on x button in that popup either implement -registerButtonTapped: method.

如果右键单击该按钮,该按钮将显示相关的动作或变量。请检查控制器中是否可用

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM