Ios 当我按下任何按钮时,我的应用程序都崩溃了
如何在工具类和工具定义中使用选择器?我的代码创建了一些错误。请告诉我如何解决此错误 错误:当我按下任何按钮时,我的应用程序崩溃Ios 当我按下任何按钮时,我的应用程序都崩溃了,ios,swift,Ios,Swift,如何在工具类和工具定义中使用选择器?我的代码创建了一些错误。请告诉我如何解决此错误 错误:当我按下任何按钮时,我的应用程序崩溃 class BarButton{ static func items(navigationItem: UINavigationItem) { //Hide Back Button navigationItem.hidesBackButton = true //Add Label to Left of Naviga
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")
}
}
您的
listbn
和notificationBtn
方法是实例方法,但您尝试在类而不是类实例上调用它们-您的项
方法是静态的
,因此它内部的self
是AnyObject.Type
类型
要修复它,请将listbn
和notificationBtn
方法设置为静态。尝试此操作并查看:
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")
}
请尝试在日志中查看崩溃消息以了解有关崩溃的详细信息。要在实用程序类中执行此操作,并在所有视图控制器中使用该类以在导航栏中显示这些按钮。NSForwarding:警告:类“DEAdmin.BarButton”的对象0x1003e7640未实现methodSignatureForSelector:--遇到故障无法识别的选择器+[DEAdmin.BarButton listBtnWithSender:]2018-04-05 16:57:38.503235+0530 DEAdmin[4381:2316382]无法识别的选择器+[DEAdmin.BarButton listBtnWithSender:]