swift 在侧面菜单中以编程方式添加开关

swift add switch programmatically in side menu

我正在尝试设置侧边菜单的样式,我希望 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: ...

这是我现在使用的 code

        //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
}

这是目前的样子 like

如何让开关编辑旁边的标签?

更改那行代码,您的选择器语法不正确:

switchStatus.addTarget(self, action: #selector(BackTableViewController.test), forControlEvents: UIControlEvents.ValueChanged)

您收到 'unrecognized selector' 错误是因为您错误地定位了 test 函数。

在这一行

switchStatus.addTarget(self, action: "test()", forControlEvents: UIControlEvents.ValueChanged)

您正在尝试在 self 上查找函数 test,但 test 是 table 视图的 viewForHeaderInSection 函数中的函数,而不是在 self.

上运行

此外,选择器语法已从字符串更改为新的 #selector 语法。

要解决此问题,请将 test 函数移至 self 上的函数。您还需要能够引用您的 switchStatusLabelStatus 对象,这样您就可以将它们移动到声明为视图控制器的成员。

所以最后你应该得到这样的结果:

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)
        // ...
    }

}