UITabBarController canBecomeFirstResponder 方法未被调用
UITabBarController canBecomeFirstResponder method not get called
阅读 http://nshipster.com/uikeycommand/ 后,我想在我的应用程序中使用箭头键在选项卡之间添加导航。但是我无法在 UITabBarController 中接收到按键。下面的相同代码块在 UIViewController 中工作,但在 UITabBarController 中工作。
当我尝试调试时,甚至没有调用 UITabBarController 的 canBecomeFirstResponder 覆盖。我没有在 apple 的文档中找到有用的东西。感谢任何使问题清晰的信息。
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
let firstResponderResult = self.becomeFirstResponder()
println("became first responder \(firstResponderResult)")
}
override func canBecomeFirstResponder() -> Bool {
return true
}
override var keyCommands: [AnyObject]? {
return [
UIKeyCommand(input: UIKeyInputRightArrow, modifierFlags: UIKeyModifierFlags.allZeros, action: "rightArrowClicked:"),
UIKeyCommand(input: UIKeyInputLeftArrow, modifierFlags: UIKeyModifierFlags.allZeros, action: "leftArrowClicked:")
]
}
func rightArrowClicked(sender: UIKeyCommand) {
let currentIndex = selectedIndex;
if (currentIndex < viewControllers!.count - 1) {
selectedIndex = currentIndex + 1;
}
}
func leftArrowClicked(sender: UIKeyCommand) {
let currentIndex = selectedIndex;
if (currentIndex > 0) {
selectedIndex = currentIndex - 1;
}
}
我认为您应该覆盖 UITabBarController 中 VC 的 canBecomeFirstResponder
方法,而不是选项卡栏控制器的方法。这样你应该能够得到按键。
iOS13 和 Mac Swift5 中的催化剂:
extension UITabBarController {
open override var keyCommands: [UIKeyCommand]? {
return [
UIKeyCommand(input: UIKeyCommand.inputRightArrow, modifierFlags: .alternate, action: #selector(rightArrowClicked)),
UIKeyCommand(input: UIKeyCommand.inputLeftArrow, modifierFlags: .alternate, action: #selector(leftArrowClicked))
]
}
@objc private func rightArrowClicked(sender: UIKeyCommand) {
let currentIndex = selectedIndex;
if (currentIndex < viewControllers!.count - 1) {
selectedIndex = currentIndex + 1;
}
}
@objc private func leftArrowClicked(sender: UIKeyCommand) {
let currentIndex = selectedIndex;
if (currentIndex > 0) {
selectedIndex = currentIndex - 1;
}
}
open override var canBecomeFirstResponder: Bool {
return true
}
阅读 http://nshipster.com/uikeycommand/ 后,我想在我的应用程序中使用箭头键在选项卡之间添加导航。但是我无法在 UITabBarController 中接收到按键。下面的相同代码块在 UIViewController 中工作,但在 UITabBarController 中工作。
当我尝试调试时,甚至没有调用 UITabBarController 的 canBecomeFirstResponder 覆盖。我没有在 apple 的文档中找到有用的东西。感谢任何使问题清晰的信息。
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
let firstResponderResult = self.becomeFirstResponder()
println("became first responder \(firstResponderResult)")
}
override func canBecomeFirstResponder() -> Bool {
return true
}
override var keyCommands: [AnyObject]? {
return [
UIKeyCommand(input: UIKeyInputRightArrow, modifierFlags: UIKeyModifierFlags.allZeros, action: "rightArrowClicked:"),
UIKeyCommand(input: UIKeyInputLeftArrow, modifierFlags: UIKeyModifierFlags.allZeros, action: "leftArrowClicked:")
]
}
func rightArrowClicked(sender: UIKeyCommand) {
let currentIndex = selectedIndex;
if (currentIndex < viewControllers!.count - 1) {
selectedIndex = currentIndex + 1;
}
}
func leftArrowClicked(sender: UIKeyCommand) {
let currentIndex = selectedIndex;
if (currentIndex > 0) {
selectedIndex = currentIndex - 1;
}
}
我认为您应该覆盖 UITabBarController 中 VC 的 canBecomeFirstResponder
方法,而不是选项卡栏控制器的方法。这样你应该能够得到按键。
iOS13 和 Mac Swift5 中的催化剂:
extension UITabBarController {
open override var keyCommands: [UIKeyCommand]? {
return [
UIKeyCommand(input: UIKeyCommand.inputRightArrow, modifierFlags: .alternate, action: #selector(rightArrowClicked)),
UIKeyCommand(input: UIKeyCommand.inputLeftArrow, modifierFlags: .alternate, action: #selector(leftArrowClicked))
]
}
@objc private func rightArrowClicked(sender: UIKeyCommand) {
let currentIndex = selectedIndex;
if (currentIndex < viewControllers!.count - 1) {
selectedIndex = currentIndex + 1;
}
}
@objc private func leftArrowClicked(sender: UIKeyCommand) {
let currentIndex = selectedIndex;
if (currentIndex > 0) {
selectedIndex = currentIndex - 1;
}
}
open override var canBecomeFirstResponder: Bool {
return true
}