如何将Core Data ManagedObjectContext的实例传递给另一个UIViewController

时间:2015-04-12 17:03:30

标签: ios cocoa-touch swift core-data uiviewcontroller

编辑:我已经在评论我的问题的人的帮助下成功解决了这个问题。非常感谢大家!我已经为每个可能与我发现自己处于同一位置的人更新了问题中的代码。

我有一个包含6个属性的User实体。

class User: NSManagedObject {

    @NSManaged var activityLevel: String
    @NSManaged var age: NSNumber
    @NSManaged var gender: String
    @NSManaged var goal: String
    @NSManaged var height: NSNumber
    @NSManaged var name: String
    @NSManaged var weight: NSNumber

    class func createInManagedObjectContext(moc: NSManagedObjectContext, name: String, age: Int, gender: String) -> User {

        let newUser = NSEntityDescription.insertNewObjectForEntityForName("User", inManagedObjectContext: moc) as! User

        newUser.name = name
        newUser.age = age
        newUser.gender = gender

        return newUser
    }

}

我想在一个VC中初始化其中三个(名称,年龄和性别),其余的在第二个VC中。

这是我的第一个视图控制器的样子。我有一个mainUser变量,我想通过prepareForSegue方法发送到第二个VC。我一直试图让这个工作,但没有尽头。这样做有什么标准做法吗?

另外......如果您需要,这是我的第一个VC:

class PersonalDataViewController: UIViewController {

    var mainUser: User?
    let managedObjectContext = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext

    // MARK: UI Elements
    @IBOutlet weak var nameInputField: CustomUITextField!
    @IBOutlet weak var ageInputField: CustomUITextField!
    @IBOutlet weak var genderSelector: UISegmentedControl!

    @IBOutlet weak var proceedButton: UIButton!

    @IBAction func proceedButtonPressed(sender: AnyObject) {

    }



    func determineGender(control: UISegmentedControl) -> String {
        var gender: String?

        switch control.selectedSegmentIndex
        {
        case 0:
            gender = "male"
        case 1:
            gender = "female"
        default:
            break;
        }
        return gender!

    }

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

        if segue.identifier == "toFitnessData" {

            var fitnessDataVC = segue.destinationViewController as! FitnessDataViewController

            fitnessDataVC.currentUser = User.createInManagedObjectContext(

                self.managedObjectContext!,
                name: nameInputField.text,
                age: ageInputField.text.toInt()!,
                gender: determineGender(genderSelector)

            )

        }

    }


    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

这是第二个感兴趣的ViewController ......无法真正看出我的错误。

class FitnessDataViewController: UIViewController {

    let managedObjectContext = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext

    @IBOutlet weak var greetingsLabel: UILabel!
    @IBOutlet weak var weightInputField: UITextField!
    @IBOutlet weak var heightInputField: UITextField!

    @IBOutlet weak var activityLevelSelector: UISegmentedControl!
    @IBOutlet weak var goalSelector: UISegmentedControl!

    @IBAction func readyButtonPressed(sender: AnyObject) {
        finishUserInitialization()
    }

    var currentUser: User?


    func finishUserInitialization() {

        if let user = currentUser as User! {

            user.weight = weightInputField.text.toInt()!
            user.height = heightInputField.text.toInt()!
            user.activityLevel = determineActivityLevel(activityLevelSelector)
            user.goal = determineGoal(goalSelector)

            println(user)

        }

    }

    func determineActivityLevel(selector: UISegmentedControl) -> String {

        var activityLevel: String?

        switch selector.selectedSegmentIndex {
        case 0:
            activityLevel = "sedetary"
        case 1:
            activityLevel = "light"
        case 2:
            activityLevel = "moderate"
        case 3:
            activityLevel = "veryActive"
        case 4:
            activityLevel = "extremelyActive"
        default:
            break;
        }

        return activityLevel!

    }

    func determineGoal(selector: UISegmentedControl) -> String {

        var goal: String?

        switch selector.selectedSegmentIndex {
        case 0:
            goal = "looseWeight"
        case 1:
            goal = "maintainWeight"
        case 2:
            goal = "gainWeight"
        default:
            break;
        }

        return goal!

    }


    override func viewDidLoad() {

        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}

0 个答案:

没有答案