我有这个协议和类,它是委托者
protocol userDetailsDelegate: AnyObject {
func didSendUserDetails ( _ object: LoginViewController)
}
class LoginViewController: UIViewController {
var details: loginDetails?
var userdelegate: userDetailsDelegate?
func loginRequestrd() {
// ALOT OF CODE ABOVE BUT THE DATA BELOW SURE COMES
guard let logdetails = try? JSONDecoder().decode(loginDetails.self, from: data) else {
// print("====")
return
}
self.details = loginDetails(status: logdetails.status, message: logdetails.message, id: logdetails.id, username: logdetails.username, email: logdetails.email, fullname: logdetails.fullname, ava: logdetails.ava)
// THE DETAILS COME AS RESPONSE NO ISSUE
self.didSendUserData()
DispatchQueue.main.async {
self.navigationController?.pushViewController(HomePageViewController(), animated: true )
}
}
func didSendUserData () {
guard let userdetails = details else {
return
}
userdelegate?.didSendUserDetails(self)
}
}
}
我也有这个类,它是代表:
class HomePageViewController: UITabBarController {
var userdetails: loginDetails?
var posttitle: String?
var loginview: LoginViewController?
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .systemBackground
UITabBar.appearance().barTintColor = .systemBackground
tabBar.tintColor = .label
DispatchQueue.main.async {
self.setupVCs()
}
loginview?.userdelegate = self
loginview?.loginRequestrd()
}
}
/ 符合 userDetailsDelegate 协议
extension HomePageViewController: userDetailsDelegate {
func didSendUserDetails(_ object: LoginViewController) {
self.userdetails = object.details
}
}
委托类中的用户详细信息为 nil 。不确定我在协议一致性方面缺少什么
当你在这里调用这个方法(
didSendUserData
)时
// THE DETAILS COME AS RESPONSE NO ISSUE
self.didSendUserData()
===
func didSendUserData () {
guard let userdetails = details else {
return
}
userdelegate?.didSendUserDetails(self)
}
userdelegate
是 nil
,您可以通过 解决此问题
self.details = loginDetails(status: logdetails.status, message: logdetails.message, id: logdetails.id, username: logdetails.username, email: logdetails.email, fullname: logdetails.fullname, ava: logdetails.ava)
// THE DETAILS COME AS RESPONSE NO ISSUE
let vc = HomePageViewController()
self.userdelegate = vc
self.didSendUserData()
DispatchQueue.main.async {
self.navigationController?.pushViewController(vc, animated: true )
}
但是你需要从登录屏幕发送数据到home,这里不需要委托,你只需要通过属性转发数据
// THE DETAILS COME AS RESPONSE NO ISSUE
//self.didSendUserData() // remove this line
DispatchQueue.main.async {
let vc= HomePageViewController(
vc. userdetails = self. details
self.navigationController?.pushViewController(), animated: true )
}