ios - calling a class shared function from action
问题描述
I have a file called SocialLogin.swift inside that I have a class with some functions see below. What I am trying to do however is have the FirstViewController.swift set up a button which has an action.
The "action" is to call SocialLogin.shared.checkLogin() However everytime I run it seems to crash giving me no real understanding on why it does this.
Where when I run the same code but instead of sending it to a shared class, i send it to a local function it works fine.
let buttonIcon = UIImage(systemName: "person.circle")
let rightBarButton = UIBarButtonItem(title: "Person", style: UIBarButtonItem.Style.done, target: self, action: #selector(SocialLogin.shared.checkLogin(_:)))
rightBarButton.image = buttonIcon
self.tabBarController?.navigationItem.rightBarButtonItem = rightBarButton
This is the SocialLogin.swift file
class SocialLogin {
static let shared = SocialLogin()
@objc func checkLogin(_ sender:UIBarButtonItem!){
print("checking now")
}
@objc func loginFacebook(){
print("test")
}
}
解决方案
Just set proper target!
let rightBarButton = UIBarButtonItem(title: "Person", style: UIBarButtonItem.Style.done, target: SocialLogin.shared, action: #selector(SocialLogin.checkLogin(_:)))
推荐阅读
- php - 尝试在 Lumen 上创建我自己的身份验证时出现“非法偏移类型”
- javascript - Alert.alert 在 react-native 中自动触发两次
- python - 如何结束线程
- r - R:创建矩阵但不能用它创建 barplott
- c# - 在 C# 中存储目录路径
- java - 检索sql日期并存储在需要localDate的java对象中
- python-3.x - 具有下限的 Numpy cumsum(矢量化)
- python - 在 BeautfulSoup 中索引多个表
- ios - 没有握手的iOS websocket
- salt-stack - Salt-minion 尝试连接旧的 salt-master 而不是新的 salt-master