无法在 IBAction 方法中手动设置 UIButton 的突出显示状态
Cannot manually set the highlighted state of a UIButton in an IBAction method
在我的 ViewController
中,我有两个 IBOutlets signUpButton
和 memberButton
,并且两个按钮都链接到相同的 signUpOrMemberButtonPressed()
IBAction 函数。我正在尝试将 highlighted
属性 设置为这 2 个按钮,以便我可以在我的 submitPressed()
IBAction 函数中相应地进行后续工作。但我注意到 signUpOrMemberButtonPressed()
和 submitPressed()
IBAction 之间的奇怪行为。点击 signUpButton
或 memberButton
后,按钮在我的 signUpOrMemberButtonPressed()
中突出显示,但在执行 submitPressed()
时,调试显示它没有突出显示。这是我的 signUpOrMemberButtonPressed()
:
@IBAction func signUpOrMemberButtonPressed(sender: AnyObject) {
var button = sender as UIButton
button.highlighted = true
if signUpButton.highlighted {
memberButton.highlighted = false
} else {
signUpButton.highlighted = false
}
if (signUpButton.highlighted) {
println("signUpButton is highlighted inside 1st button")
} else if (memberButton.highlighted) {
println("memberButton is highlighted inside 1st button")
} else {
println("nothing is highlighted inside 1st button")
}
}
我的submitPressed
函数:
@IBAction func submitPressed(sender: AnyObject) {
if (signUpButton.highlighted) { println("signUpButton is highlighted inside 2st button") }
else if (memberButton.highlighted) { println("memberButton is highlighted inside 2st button") }
else { println("nothing is highlighted inside 2nd button")
}
当 运行 我的应用程序时,我点击了 memberButton
,然后点击了提交按钮。这是日志输出:
memberButton is highlighted inside 1st button
nothing is highlighted inside 2nd button
在这两个函数调用之间 运行 没有设置任何内容。
这里发生的是按钮 "un-highlighting" 本身(在您手动设置 highlighted = true 之后)。
来自documentation的highlighted
属性:
UIControl automatically sets and clears this state automatically when
a touch enters and exits during tracking and when there is a touch up.
您可以手动设置突出显示状态,但您必须在 UIButton 取消设置突出显示状态后执行此操作。您必须在下一个 运行 循环中执行此操作,您可以使用 dispatch_async
.
以下应该有效:
var button = sender as UIButton
dispatch_async(dispatch_get_main_queue()) {
self.memberButton.highlighted = button == self.memberButton
self.signUpButton.highlighted = button == self.signUpButton
}
在我的 ViewController
中,我有两个 IBOutlets signUpButton
和 memberButton
,并且两个按钮都链接到相同的 signUpOrMemberButtonPressed()
IBAction 函数。我正在尝试将 highlighted
属性 设置为这 2 个按钮,以便我可以在我的 submitPressed()
IBAction 函数中相应地进行后续工作。但我注意到 signUpOrMemberButtonPressed()
和 submitPressed()
IBAction 之间的奇怪行为。点击 signUpButton
或 memberButton
后,按钮在我的 signUpOrMemberButtonPressed()
中突出显示,但在执行 submitPressed()
时,调试显示它没有突出显示。这是我的 signUpOrMemberButtonPressed()
:
@IBAction func signUpOrMemberButtonPressed(sender: AnyObject) {
var button = sender as UIButton
button.highlighted = true
if signUpButton.highlighted {
memberButton.highlighted = false
} else {
signUpButton.highlighted = false
}
if (signUpButton.highlighted) {
println("signUpButton is highlighted inside 1st button")
} else if (memberButton.highlighted) {
println("memberButton is highlighted inside 1st button")
} else {
println("nothing is highlighted inside 1st button")
}
}
我的submitPressed
函数:
@IBAction func submitPressed(sender: AnyObject) {
if (signUpButton.highlighted) { println("signUpButton is highlighted inside 2st button") }
else if (memberButton.highlighted) { println("memberButton is highlighted inside 2st button") }
else { println("nothing is highlighted inside 2nd button")
}
当 运行 我的应用程序时,我点击了 memberButton
,然后点击了提交按钮。这是日志输出:
memberButton is highlighted inside 1st button
nothing is highlighted inside 2nd button
在这两个函数调用之间 运行 没有设置任何内容。
这里发生的是按钮 "un-highlighting" 本身(在您手动设置 highlighted = true 之后)。
来自documentation的highlighted
属性:
UIControl automatically sets and clears this state automatically when a touch enters and exits during tracking and when there is a touch up.
您可以手动设置突出显示状态,但您必须在 UIButton 取消设置突出显示状态后执行此操作。您必须在下一个 运行 循环中执行此操作,您可以使用 dispatch_async
.
以下应该有效:
var button = sender as UIButton
dispatch_async(dispatch_get_main_queue()) {
self.memberButton.highlighted = button == self.memberButton
self.signUpButton.highlighted = button == self.signUpButton
}