Im trying to style my side menu, I want the UISwitch
to edit the label next to it. But the only think that happens when I click on the switch is that the app crashes and I receives following error message
我试图设置我的侧面菜单,我希望UISwitch编辑旁边的标签。但是,当我点击开关时发生的唯一想法是应用程序崩溃,我收到以下错误消息
2016-08-20 01:56:20.545 Moppio[4342:8394913] -[Moppio.BackTableViewController test()]: unrecognized selector sent to instance 0x7fe66a4ba530 2016-08-20 01:56:20.554 Moppio[4342:8394913] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Moppio.BackTableViewController test()]: unrecognized selector sent to instance 0x7fe66a4ba530' * First throw call stack: ...
2016-08-20 01:56:20.545 Moppio [4342:8394913] - [Moppio.BackTableViewController test()]:无法识别的选择器发送到实例0x7fe66a4ba530 2016-08-20 01:56:20.554 Moppio [4342:8394913] *终止应用程序由于未捕获的异常'NSInvalidArgumentException',原因:' - [Moppio.BackTableViewController test()]:无法识别的选择器发送到实例0x7fe66a4ba530'*第一次抛出调用堆栈:...
And this is the code
that I use right now.
这是我现在使用的代码。
//Profile picture + bar
override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?{
let view = UIView(frame: CGRectMake(0, 0, tableView.bounds.size.width, 60))
view.backgroundColor = UIColor(red:0.40, green:0.40, blue:0.40, alpha:1.0)
let profileImageView = UIImageView(frame: CGRectMake(20, 5, 50, 50)) // Change frame size according to you...
profileImageView.image = UIImage(named: "profile.png") //Image set your
view.addSubview(profileImageView)
let LabelStatus = UILabel(frame: CGRectMake(0, 0, 200, 21))
LabelStatus.center = CGPointMake(120, 32)
LabelStatus.textColor = UIColor(red:0.95, green:0.95, blue:0.95, alpha:1.0)
LabelStatus.font = UIFont(name: "BebasNeue", size: 24)
LabelStatus.textAlignment = NSTextAlignment.Center
self.view.addSubview(LabelStatus)
let switchStatus=UISwitch(frame:CGRectMake(195, 15, 20, 20))
func test() {
if (switchStatus.on == true){
LabelStatus.text = ("Available")
print("on")
}
if (switchStatus.on == false){
LabelStatus.text = ("Not available")
LabelStatus.center = CGPointMake(130, 32)
print("off")
}
}
switchStatus.on = true
switchStatus.setOn(true, animated: false);
switchStatus.addTarget(self, action: "test()", forControlEvents: UIControlEvents.ValueChanged)
switchStatus.onTintColor = UIColor(red:0.95, green:0.95, blue:0.95, alpha:1.0)
switchStatus.thumbTintColor = UIColor(red:0.25, green:0.25, blue:0.25, alpha:1.0)
self.view.addSubview(switchStatus);
return view
}
And this is what it currently looks like.
这就是它目前的样子。
How can I make the switch edit the label next to it?
如何让开关编辑旁边的标签?
2 个解决方案
#1
1
You are getting the 'unrecognized selector' error because you are incorrectly targeting the test
function.
您收到“无法识别的选择器”错误,因为您错误地定位了测试功能。
In this line
在这一行
switchStatus.addTarget(self, action: "test()", forControlEvents: UIControlEvents.ValueChanged)
You are trying to find a function test
on self
, but test
is a function within the table view's viewForHeaderInSection
function, not a function on self
.
您正在尝试在self上找到函数测试,但test是表视图的viewForHeaderInSection函数中的函数,而不是self上的函数。
Additionally, the selector syntax has changed from a String to the new #selector
syntax.
此外,选择器语法已从String更改为新的#selector语法。
To fix this, move the test
function to be a function on self
. You will also need to be able to reference your switchStatus
and LabelStatus
objects, so you could move these to be declared as members of your view controller as well.
要解决此问题,请将测试功能移至自我的功能。您还需要能够引用switchStatus和LabelStatus对象,因此您可以将这些对象移动为视图控制器的成员。
So at the end you should end up with something like this:
所以最后你应该得到这样的东西:
class BackTableViewController: UITableViewController {
let LabelStatus = UILabel()
let switchStatus = UISwitch()
func test() {
// update the label here
}
override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?{
// ...
switchStatus.addTarget(self, action: #selector(BackTableViewController.test), forControlEvents: UIControlEvents.ValueChanged)
// ...
}
}
#2
0
Change that line of code, your syntax for the selector is not correct:
更改该行代码,您的选择器语法不正确:
switchStatus.addTarget(self, action: #selector(BackTableViewController.test), forControlEvents: UIControlEvents.ValueChanged)
#1
1
You are getting the 'unrecognized selector' error because you are incorrectly targeting the test
function.
您收到“无法识别的选择器”错误,因为您错误地定位了测试功能。
In this line
在这一行
switchStatus.addTarget(self, action: "test()", forControlEvents: UIControlEvents.ValueChanged)
You are trying to find a function test
on self
, but test
is a function within the table view's viewForHeaderInSection
function, not a function on self
.
您正在尝试在self上找到函数测试,但test是表视图的viewForHeaderInSection函数中的函数,而不是self上的函数。
Additionally, the selector syntax has changed from a String to the new #selector
syntax.
此外,选择器语法已从String更改为新的#selector语法。
To fix this, move the test
function to be a function on self
. You will also need to be able to reference your switchStatus
and LabelStatus
objects, so you could move these to be declared as members of your view controller as well.
要解决此问题,请将测试功能移至自我的功能。您还需要能够引用switchStatus和LabelStatus对象,因此您可以将这些对象移动为视图控制器的成员。
So at the end you should end up with something like this:
所以最后你应该得到这样的东西:
class BackTableViewController: UITableViewController {
let LabelStatus = UILabel()
let switchStatus = UISwitch()
func test() {
// update the label here
}
override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?{
// ...
switchStatus.addTarget(self, action: #selector(BackTableViewController.test), forControlEvents: UIControlEvents.ValueChanged)
// ...
}
}
#2
0
Change that line of code, your syntax for the selector is not correct:
更改该行代码,您的选择器语法不正确:
switchStatus.addTarget(self, action: #selector(BackTableViewController.test), forControlEvents: UIControlEvents.ValueChanged)