我试图在SpringApp中创建类似于Code Menu的滑动菜单(参见CodeViewController):
https://github.com/MengTo/Spring/blob/master/SpringApp
只要SpringViewController是初始视图控制器,它就可以正常工作。
如果我创建另一个ViewController并使其初始化,那么就不会调用minimizeView / maximizeView:
UIApplication.sharedApplication().sendAction("minimizeView:", to: nil, from: self, forEvent: nil)
用这种方法"来:"设置为nil以使用第一响应者。所以看起来当SpringViewController不再是初始视图控制器时,它不再是第一响应者。
如何修复它以使在SpringViewController中定义的minimizeView和maximizeView动作始终有效?
答案 0 :(得分:0)
我找到了使用通知中心的解决方法。
在SpringViewController中添加:
override func viewDidLoad() {
super.viewDidLoad()
// Add observer in notification center
// It receives notifications from menu
NSNotificationCenter.defaultCenter().addObserver(
self,
selector: "minimizeView:",
name: "minimizeView",
object: nil)
NSNotificationCenter.defaultCenter().addObserver(
self,
selector: "maximizeView:",
name: "maximizeView",
object: nil)
}
在OptionsViewController中:
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(true)
// skipped ...
// Post notification
NSNotificationCenter.defaultCenter().postNotificationName("minimizeView", object: nil)
}
@IBAction func closeButtonPressed(sender: AnyObject) {
// skipped...
// Post notification
NSNotificationCenter.defaultCenter().postNotificationName("maximizeView", object: nil)
// skipped...
}
这使maximizeView和minimizeView正常工作。我想知道第一响应者的方法是否更好。