简体   繁体   English

如果不是Initial View Controller,则无法将操作发送给第一响应者

[英]Send action to first responder doesn't work if it's not Initial View Controller

I'm trying to create sliding menu similar to Code Menu in SpringApp here (see CodeViewController): 我正在尝试在此处创建类似于SpringApp中的代码菜单的滑动菜单(请参见CodeViewController):

https://github.com/MengTo/Spring/blob/master/SpringApp https://github.com/MengTo/Spring/blob/master/SpringApp

It works just fine as long as SpringViewController is Initial View Controller. 只要SpringViewController是Initial View Controller,它就可以正常工作。

If I create another ViewController and make it initial then minimizeView/maximizeView is never getting called: 如果我创建另一个ViewController并将其设置为初始,则将不会调用minimumView / maximizeView:

UIApplication.sharedApplication().sendAction("minimizeView:", to: nil, from: self, forEvent: nil)

In this method "to:" is set to nil to use the first responder. 在此方法中,将“ to:”设置为nil以使用第一响应者。 So it looks like when SpringViewController stops being initial view controller it's no longer first responder. 因此,当SpringViewController不再是初始视图控制器时,它不再是第一响应者。

How do I fix it to make minimizeView and maximizeView actions defined in SpringViewController always work? 如何解决它,以使SpringViewController中定义的minimalView和maximumView动作始终起作用?

I found a workaround using notification center. 我发现了使用通知中心的解决方法。

In SpringViewController add: 在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)
  }

In OptionsViewController: 在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...
}

This makes maximizeView and minimizeView work properly. 这使得maximateView和minimumView可以正常工作。 I wonder if the approach with first responder is better. 我想知道第一响应者的方法是否更好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM