简体   繁体   中英

AppDelegate issues

I'm working on my first project on Swift. I'm new to iOS itself.

I create various screens like register, sign in etc. using storyboard. The Register screen is linked from the Sign In screen. But the app crashes as soon as I tap on 'Register' link in the Sign In screen.

I haven't defined anything in the default view controller. However, I've created the register user view controller where I've defined App Delegate. Below is my code for Register user view controller:

class RegisterUserViewController: UIViewController {


    @IBOutlet weak var FFName: UITextField!
    @IBOutlet weak var FEmail: UITextField!
    @IBOutlet weak var FPassword: UITextField!
    @IBOutlet weak var FAge: UITextField!
    @IBOutlet weak var FGender: UITextField!
    @IBOutlet weak var FLocation: UITextField!


    @IBAction func registerUser(sender: UIButton) {
        //define an app delegate
        //define a managed context
        //define an entity description
        //perform entity operation like add, delete, search
        //save the context

        var appDel = UIApplication.sharedApplication().delegate as AppDelegate
        var context:NSManagedObjectContext = appDel.managedObjectContext!
        var newUsr = NSEntityDescription.insertNewObjectForEntityForName("Users", inManagedObjectContext: context) as NSManagedObjectContext

        newUsr.setValue("myusername", forKey: "username")
        newUsr.setValue(FFName, forKey: "fullname")
        newUsr.setValue(FEmail.text, forKey: "email")
        newUsr.setValue(FPassword.text, forKey: "password")
        newUsr.setValue(FAge, forKey: "age")
        newUsr.setValue(FGender, forKey: "gender")
        newUsr.setValue(FLocation, forKey: "location")

        context.save(nil)
        println(FFName.text)
        println(FEmail.text)
    }

Could you please suggest if this is the right way to work with App Delegates or do I need to define anything in AppDelegate.swift?

Appreciate any information.

Thanks

NOTE: GENERAL ADVICE

You are referencing the AppDelegate from the ViewController to get the context.

This works but creates a dependency of the VC to the appDelegate... not the nicest thing :D

two alternatives: - give the view controller a var ctx and pass it to it from the appDelegate - make a DataStoreManager class that has the CD stack. (That moves this code out of the appDelegate which is ideal IMHO - setting up a CD stack doesn't really belong into a controller)

in the end though: it ALSO comes down to personal preference

Please delete the register button and agin add the register button.. may be previously declare action is there and after that you have changes the function name so this kind of crash accrued.

Solutions

  • Right click on button and see the already bind action method if there is unnecessary method than remove it.
  • Remove button and again add new button and bind action 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