我正在尝试在popoverpresentationviewcontroller中使用coredata,但一直说我的名为PopoverVC的视图控制器没有名为managedObjectContext的成员。

我努力了:

1.初始化PopoverViewController的内部初始化:

required init(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)

    self.appDelegate = (UIApplication.sharedApplication().delegate) as! AppDelegate
    self.managedObjectContext = appDelegate.managedObjectContext!
}

2.从另一个我通过调用App Delegate 获得的视图控制器传递它
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
    switch(segue.identifier!) {
        case "popOverSegue":
            let controller = segue.destinationViewController as! PopoverVC
            controller.managedObjectContext  = self.managedObjectContext!
            break
        default:
            break
     }
}

通常,我可以使用以下命令在任何其他视图控制器中访问它:
((UIApplication.sharedApplication().delegate) as! AppDelegate).managedObjectContext

我听说这可能是不好的作法,但这在此Popover视图控制器中甚至不起作用。其他关于stackoverflow问题的解决方案也不起作用。

编辑,下面是我的PopoverViewController类的样子,包括所有内容以确保它不是在破坏代码的地方出现愚蠢的语法错误。我现在将managedObjectContext声明为getter属性。还是一样的错误。
class PopoverViewController: UIViewController, UIPopoverPresentationControllerDelegate, UIPickerViewDelegate {

@IBOutlet weak var testingLabel: UILabel!
@IBOutlet weak var aPicker: UIPickerView!

required init(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)

    //cancel button
    navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Cancel, target: self, action: "tapCancel:")

    // popover settings
    modalPresentationStyle = .Popover
    popoverPresentationController!.delegate = self

    self.preferredContentSize = CGSize(width:400,height:600)
}

//var appDelegate: AppDelegate
//var managedObjectContext:NSManagedObject
var appDelegate: AppDelegate {
    return UIApplication.sharedApplication().delegate as! AppDelegate
}

var managedObjectContext: NSManagedObjectContext {
    return self.appDelegate.managedObjectContext!
}

var pickerData = ["Mozzarella","Gorgonzola","Provolone","Brie","Maytag Blue","Sharp Cheddar","Monterrey Jack","Stilton","Gouda","Goat Cheese", "Asiago"]

let fetchRequest = NSFetchRequest(entityName: "Region")


var fetchResults = managedObjectContext!.executeFetchRequest(fetchRequest, error: nil) as [Region]
//pickerData = fetchResults

func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int{
    return 2
}

func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int{
    return pickerData.count
}

func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String!{
    //return pickerData[row]
}

@IBInspectable var popoverOniPhone:Bool = false
@IBInspectable var popoverOniPhoneLandscape:Bool = true

func adaptivePresentationStyleForPresentationController(PC: UIPresentationController) -> UIModalPresentationStyle{

    //this methods is only called by System when the screen has compact width

    //return .None means we still want popover when adaptive on iPhone
    //return .FullScreen means we'll get modal presetaion on iPhone

    switch(popoverOniPhone, popoverOniPhoneLandscape){
    case (true, _): // always popover on iPhone
        return .None

    case (_, true): // popover only on landscape on iPhone
        let size = PC.presentingViewController.view.frame.size
        if(size.width>320.0){ //landscape
            return .None
        }else{
            return .FullScreen
        }

    default: // no popover on iPhone
        return .FullScreen
    }


}

func presentationController(_: UIPresentationController, viewControllerForAdaptivePresentationStyle _: UIModalPresentationStyle)
    -> UIViewController?{
        return UINavigationController(rootViewController: self)
}

func tapCancel(_ : UIBarButtonItem) {
    //tap cancel
    dismissViewControllerAnimated(true, completion:nil);
}

}

最佳答案

您已将managedObjectContext声明为只读的计算属性,因此无法为其分配值。

要使用prepareForSegue中的赋值,您需要使其成为简单明了的可选属性-

var managedObjectContext: NSManagedObjectContext?

关于ios - ViewController没有名为managedObjectContext的成员,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31780169/

10-11 08:46