我正在尝试在popoverpresentationviewcontroller中使用coredata,但它一直说我的视图控制器名为PopoverVC没有名为managedObjectContext的成员。
我试过了:
1。在PopoverViewController的init内部初始化:
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);
}
}
答案 0 :(得分:0)
您已将managedObjectContext
声明为只读计算属性,因此您无法为其指定值。
要使用prepareForSegue
中的作业,您需要将其作为直接的可选属性 -
var managedObjectContext: NSManagedObjectContext?