ios - 在侧面菜单中以编程方式快速添加开关

标签 ios swift error-handling add uiswitch

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

这是我现在使用的代码

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

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

最佳答案

您收到“无法识别的选择器”错误,因为您错误地定位了 test功能。

在这一行

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

您正在尝试查找函数 testself ,但是 test是 TableView 中的一个函数 viewForHeaderInSection函数,不是 self 上的函数.

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

要解决此问题,请移动 test函数成为 self 上的函数.您还需要能够引用您的 switchStatusLabelStatus对象,因此您也可以移动它们以将其声明为 View Controller 的成员。

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

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

}

关于ios - 在侧面菜单中以编程方式快速添加开关,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39049185/

相关文章:

iOS 自定义初始化问题,如何设置默认初始化方法?

ios - 如何在 SwiftUI 中实现协议(protocol)和委托(delegate)

ios - 警告 "Mapping architecture arm64 to x86_64"是什么意思?

c - 为什么是 "strcat already defined in ***.obj"?

javascript - Sendgrid node.js 错误 535 错误的用户名/密码

ios - 在 Objective-C 中创建方波、锯齿波和三角波

ios - 没有版本控制的 CoreData 迁移

ios - 无法通过 iOS 应用程序发布到 Facebook 的深层链接

ios - SwiftUI 和三指撤消手势

c# - "CS1513: } Expected"错误