简体   繁体   English

使用Swift 4 App(Xcode 9.X)在iOS 11中缺少第一响应者吗?

[英]Missing First Responder in iOS 11 with Swift 4 App (Xcode 9.X)?

I have a stripped down, code based test app (no Storyboard ), that has: 我有一个精简的,基于代码的测试应用程序(没有Storyboard ),它具有:

  • UIWindow object UIWindow对象
  • ViewController, with its main View ViewController及其主视图
  • Single UIView object on top. 单个UIView对象位于顶部。 - -

To test which object is the first responder, i evaluate in tochesBegan , inside the topView as follows, but all items respond false . 为了测试哪个对象是第一个响应者,我按以下方式在tochesBegan内的tochesBegan进行评估,但是所有项目的响应均为false The Responder Chain is working because the touch is detected (both in the topView and in the main controller's view. 响应程序链之所以起作用,是因为在topView和主控制器的视图中都检测到了触摸。

This is the stripped down version of the app. 这是该应用程序的精简版本。 Given that this is not a simple view-subview hierarchy, I don't use a recursive function. 由于这不是一个简单的视图-子视图层次结构,因此我不使用递归函数。

import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    var window: UIWindow?
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        window = UIWindow()
        window?.rootViewController = ViewController()
        window?.makeKeyAndVisible()
        return true
    }
}
class ViewController: UIViewController {
    override func viewDidLoad() {
        let topView = View()
        self.view.addSubview(topView)
    }
}
class View: UIView {
    convenience init() {
        self.init(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
        self.backgroundColor = UIColor.yellow
    }
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        let window = (UIApplication.shared).delegate?.window as? UIWindow
        let viewController = window?.rootViewController
        let mainView = viewController?.view
        print("Window?: \(window!.isFirstResponder)")
        print("View Controller?: \(viewController!.isFirstResponder)")
        print("Main View?: \(mainView!.isFirstResponder)")
        for view in (mainView?.subviews)! {
            print("Any View?: \(view.isFirstResponder)")
        }
    }
}

All evaluations return false. 所有评估均返回false。 Am I doing something wrong here? 我在这里做错什么了吗?

Regards... e 问候... e

I believe without subclassing there are few view objects (UITextField, UITextView) that can become first responder, its better you check if UIView canBecomeFirstResponder . 我相信如果没有子类,很少有视图对象(UITextField,UITextView)可以成为第一响应者,最好检查一下UIView canBecomeFirstResponder

If you want to achieve this functionality, such as highlighting, selecting of a UIView, you have to subclass the UIView and override the becomeFirstResponder , as following: 如果要实现此功能(例如突出显示,选择UIView),则必须对UIView进行子类化并重写becomeFirstResponder ,如下所示:

class CustomView: UIView {

    override var isFirstResponder: Bool {
        return true
    }

}

To test above, add following in your ViewController: 要进行上述测试,请在ViewController中添加以下内容:

override func loadView() {
    self.view = CustomView()
}

Now check to see if our CustomView isFirstResponder: 现在检查一下我们的CustomView是否为FirstResponder:

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    print("View: \(self.view.isFirstResponder)")
}

I think there is no first responder from start. 我认为从一开始就没有第一响应者。 You can still receive events to your views. 您仍然可以接收事件到您的视图。

Responder object 响应者对象
Mouse events and multitouch events first go to the view that is under the mouse pointer or finger; 鼠标事件和多点触摸事件首先进入鼠标指针或手指下方的视图; that view might or might not be the first responder. 该视图可能是也可能不是第一响应者。

More on responders 有关响应者的更多信息

nextResponder nextResponder
The UIResponder class does not store or set the next responder automatically, so this method returns nil by default. UIResponder类不会自动存储或设置下一个响应者,因此默认情况下此方法返回nil。

More on nextResponder 更多关于nextResponder

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

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