缺少 iOS 11 中的第一响应者和 Swift 4 应用程序 (Xcode 9.X)?
Missing First Responder in iOS 11 with Swift 4 App (Xcode 9.X)?
我有一个精简的、基于代码的测试应用程序(没有 Storyboard
),它有:
UIWindow
对象
- ViewController,其主视图
- 单个
UIView
对象在顶部。 -
为了测试哪个对象是第一响应者,我在 tochesBegan
中对 topView 进行了如下评估,但所有项目都响应 false
。响应链正在工作,因为检测到触摸(在 topView 和主控制器的视图中。
这是该应用程序的精简版。鉴于这不是一个简单的视图-子视图层次结构,我不使用递归函数。
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)")
}
}
}
所有评价return 错误。我是不是做错了什么?
问候... e
我相信如果没有子类化,很少有视图对象(UITextField、UITextView)可以成为第一响应者,最好检查一下 UIView
canBecomeFirstResponder
.
如果你想实现这个功能,比如突出显示,选择一个 UIView,你必须继承 UIView 并覆盖 becomeFirstResponder
,如下所示:
class CustomView: UIView {
override var isFirstResponder: Bool {
return true
}
}
要进行上述测试,请在 ViewController 中添加以下内容:
override func loadView() {
self.view = CustomView()
}
现在检查我们的 CustomView 是否是 FirstResponder:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
print("View: \(self.view.isFirstResponder)")
}
我认为从一开始就没有第一响应者。您仍然可以收到您的意见的事件。
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.
nextResponder
The UIResponder class does not store or set the next responder
automatically, so this method returns nil by default.
我有一个精简的、基于代码的测试应用程序(没有 Storyboard
),它有:
UIWindow
对象- ViewController,其主视图
- 单个
UIView
对象在顶部。 -
为了测试哪个对象是第一响应者,我在 tochesBegan
中对 topView 进行了如下评估,但所有项目都响应 false
。响应链正在工作,因为检测到触摸(在 topView 和主控制器的视图中。
这是该应用程序的精简版。鉴于这不是一个简单的视图-子视图层次结构,我不使用递归函数。
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)")
}
}
}
所有评价return 错误。我是不是做错了什么?
问候... e
我相信如果没有子类化,很少有视图对象(UITextField、UITextView)可以成为第一响应者,最好检查一下 UIView
canBecomeFirstResponder
.
如果你想实现这个功能,比如突出显示,选择一个 UIView,你必须继承 UIView 并覆盖 becomeFirstResponder
,如下所示:
class CustomView: UIView {
override var isFirstResponder: Bool {
return true
}
}
要进行上述测试,请在 ViewController 中添加以下内容:
override func loadView() {
self.view = CustomView()
}
现在检查我们的 CustomView 是否是 FirstResponder:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
print("View: \(self.view.isFirstResponder)")
}
我认为从一开始就没有第一响应者。您仍然可以收到您的意见的事件。
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.
nextResponder
The UIResponder class does not store or set the next responder automatically, so this method returns nil by default.