简体   繁体   English

当我触摸任何按钮时,我的应用程序崩溃了

[英]My app was crashed when i touched on any button

How to use the selector in Utility Class and there definition? 如何在Utility Class中使用选择器并定义? My code create some error. 我的代码创建了一些错误。 PLease tell me how to resolve this error? 请告诉我如何解决此错误?

Error : My app crashed when I touched on any button. 错误:当我触摸任何按钮时,我的应用程序崩溃了。

class BarButton{
    static func items(navigationItem: UINavigationItem) {
        //Hide Back Button
        navigationItem.hidesBackButton = true

        //Add Label to Left of NavigationBar
        let leftLabel = UIBarButtonItem(title: "", style: .plain, target: self, action: nil)
        leftLabel.tintColor = UIColor.init(red: 30/255, green: 104/255, blue: 140/255, alpha: 1)
        navigationItem.setLeftBarButton(leftLabel, animated: false)

        //List Button
        let btnList = UIButton(frame: CGRect(x: 0, y: 0, width: 35, height: 22))
        btnList.setImage(UIImage(named:"list-icn"), for: .normal)
        btnList.addTarget(self, action: #selector(listBtn), for: .touchUpInside)
        let rightBarBtn1 = UIBarButtonItem.init(customView: btnList)
        rightBarBtn1.tintColor = UIColor(red: 30/255, green: 104/255, blue: 140/255, alpha: 1)

        //Notification Button
        let btnList2 = UIButton(frame: CGRect(x: 0, y: 0, width: 22, height: 22))
        btnList2.setImage(UIImage(named:"notification-icn"), for: .normal)
        btnList2.addTarget(self, action: #selector(notificationBtn), for: .touchUpInside)
        let rightBarBtn2 = UIBarButtonItem.init(customView: btnList2)
        rightBarBtn2.tintColor = UIColor(red: 30/255, green: 104/255, blue: 140/255, alpha: 1)
        navigationItem.setRightBarButtonItems([rightBarBtn1, rightBarBtn2], animated: true)
    }

    @objc func listBtn() {
        print("List Btn")
    }

    @objc func notificationBtn() {
        print("Notification Btn")
    }
}

Try this and see: 试试看,看看:

let btnList = UIButton(frame: CGRect(x: 0, y: 0, width: 35, height: 22))
btnList.setImage(UIImage(named:"list-icn"), for: .normal)

// Update selector
btnList.addTarget(self, action: #selector(BarButton.listBtn(sender:)), for: .touchUpInside)
let rightBarBtn1 = UIBarButtonItem.init(customView: btnList)
rightBarBtn1.tintColor = UIColor(red: 30/255, green: 104/255, blue: 140/255, alpha: 1)


//Notification Button
let btnList2 = UIButton(frame: CGRect(x: 0, y: 0, width: 22, height: 22))
btnList2.setImage(UIImage(named:"notification-icn"), for: .normal)

// Update selector
btnList2.addTarget(self, action: #selector(BarButton.notificationBtn(sender:)), for: .touchUpInside)
let rightBarBtn2 = UIBarButtonItem.init(customView: btnList2)
rightBarBtn2.tintColor = UIColor(red: 30/255, green: 104/255, blue: 140/255, alpha: 1)
navigationItem.setRightBarButtonItems([rightBarBtn1, rightBarBtn2], animated: true)


// Try using static
@objc static func listBtn(sender: Any)
{
    print("List Btn")
}

// Try using static
@objc static func notificationBtn(sender: Any)
{
    print("Notification Btn")
}

Your listBtn and notificationBtn methods are instance methods, but you try to invoke them on a class, not class instance - your items method is static , so self inside of it is of AnyObject.Type type. 您的listBtnnotificationBtn方法是实例方法,但是您尝试在类上调用它们,而不是类实例 - 您的items方法是static ,因此它内部的selfAnyObject.Type类型。

To fix it, make listBtn and notificationBtn methods static. 要修复它, listBtnnotificationBtn方法listBtn静态。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我按注册按钮时,我的应用程序崩溃了,但数据已成功插入数据库中 - When I press register button my app has crashed but data insert successfully in database 在Swift中触摸按钮会使应用崩溃 - Button Crashes App When Touched In Swift 当我创建 DDXMLDocument 的自动释放 object 时,我的应用程序崩溃了 - My app crashed when I created autorelease object of DDXMLDocument 有没有办法检测它在Apple Pay PKPaymentAuthorizationViewController上触摸的取消按钮? - Is there any way to detect when the Cancel button it touched on an Apple Pay PKPaymentAuthorizationViewController? 当我的ViewController导航应用程序崩溃时AFURLSessionManager - AFURLSessionManager when my ViewController was navigated app crashed 当我从TableView上的按钮操作中删除单元格时,应用程序崩溃了-iOS Swift - When i remove cell from TableView on button action the app is crashed - iOS Swift iOS:当我从测试精灵安装时,我的应用程序在 iPhone 中崩溃了,但在 iPad 中它工作正常 - iOS: My app crashed in iPhone when i am installing from test fairy but in iPad it's working fine 删除核心数据实体的最后一条记录时,为什么我的应用程序崩溃了? - Why do my app crashed when I delete the last record of a core data entity? PresentViewController使我的应用程序崩溃 - PresentViewController crashed my app 触摸时更改按钮的颜色 - Changing color of button when touched
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM