UIViewController的委托var变为零

时间:2016-07-19 20:52:19

标签: ios swift delegates

我已阅读所有文章但未找到解决方案。我有一个班级来组织控制器之间的导航:

myCanvas.toBlob(function(myBlob) {
  // send blob to server here!!
}, "image/jpeg", 0.5);

}

我的class Navigator: NotesListViewControllerDelegate { var rootController:UINavigationController! lazy private var notesListController:NotesListViewController! = { let controller = NotesListViewController(nibName: String(NotesListViewController), bundle: nil) controller.title = "Notes" return controller }() init() { self.rootController = UINavigationController.init(rootViewController: self.notesListController) self.notesListController.delegate = self } //MARK: NotesListController delegate func plusButtonDidSelect() { print("plus button did select") }

NotesListController

系统在protocol NotesListViewControllerDelegate:class { func plusButtonDidSelect() } class NotesListViewController: UIViewController { weak var delegate:NotesListViewControllerDelegate? { didSet { print("delegate = \(delegate)") } } 中显示已设置的变量。但在didSet viewDidLoad中已经有self.delegate值。可能有什么问题?

编辑:我的nil方法:

didFinishLaunchingWithOptions

编辑2 我通过从func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { self.window = UIWindow.init(frame: UIScreen.mainScreen().bounds) let navigator = Navigator() if let window = self.window { window.backgroundColor = UIColor.whiteColor() window.rootViewController = navigator.rootController window.makeKeyAndVisible() } return true } 方法中取出navigator变量到其他类变量来解决了这个问题(它变成了obj-c中的强属性)。但是当系统在didFinishLaunchingWithOptions方法中声明为localy时,为什么系统不会将其保留在内存中?

1 个答案:

答案 0 :(得分:2)

什么是维护对Navigator实例的引用?

NotesListViewController实例只有一个弱引用,所以如果没有别的东西让Navigator保持活动状态,它将超出范围,并且委托变量将在notesListController中变为nil(我假设它在其他地方被引用,因为它正在呈现)< / p>