我正在尝试在一个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. 从另一个视图控制器传递,我只需从调用应用程序委托获取它
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
我听说这可能是不好的做法,但是在这个弹出视图控制器中甚至无法工作。其他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
? - Paulw11PopoverViewController
的类定义,包括此属性定义,因为编译器显示您没有它。 - Paulw11