簡體   English   中英

Swift 3中的協議和委派在視圖控制器之間不發送值

[英]Protocols and delegation in swift 3 not sending values between viewcontrollers

我有兩個viewcontrollers( SignInViewcontroller.swiftProfilePage.swift ),我希望將字符串從SignInViewcontrollerProfilePage viewcontroller 我在SignInViewcontroller創建了一個協議, SignInViewcontrollerProfilePage controller委派了該方法。當我通過協議發送字符串時,我在ProfilePage viewcontroller沒有收到該字符串。我錯了。請幫我解決。 這是我的代碼:

SignInViewController.swift

protocol sendTokenDelegate: class {
    func sendToken(login:String)
}
class SignInViewController: UIViewController  {
    weak var delegateToken:sendTokenDelegate?
    func loginAzure(email: String, password: String) {
        token = "abcdefgh"
        self.delegateToken?.sendToken(login: token)
    }
}

ProfilePage.swift

class ProfilePage: UIViewController, UITableViewDelegate, UITableViewDataSource, sendTokenDelegate {
    override func viewDidLoad() {
        let signInVC = SignInViewController()
        signInVC.delegateToken = self
    }
    func sendToken(login: String) {
        self.logInToken = login
        print("Login Token in Profile Page is \(login)")
    }
}

如果您是從SignUpViewController到ProfilePageViewController的,您可以在從logingAzure()獲取所需的singIn令牌后,在導航時傳遞字符串值,我假設:

  1. 如果您使用segues-> self.performSegue(withIdentifier:“ signUpToProfile”,發件人:self)進行導航

     override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if segue.identifier == "signUpToProfile" { if let profileVC : ProfilePageViewController = segue.destination as? ProfilePageViewController { profileVC.loginToken = token } } } 
  2. 如果您使用的是self.navigationController?.pushViewController

     let storyboard = UIStoryboard(name: "Profile", bundle: Bundle.main) if let profileVC = storyboard.instantiateViewController(withIdentifier: "ProfilePageViewController") as? ProfilePageViewController { profileVC.loginToken = token } 

編輯

如果您不打算直接從SignUpViewController轉到profilePage,則只需將令牌保存在“鑰匙串”或“ UserDefaults”中。

通過創建SessionManager單例來執行此操作,以在登錄或注冊時處理令牌和其他資源

在這種情況下,您需要一個對象,該對象將存儲SignInViewController中的令牌,直到ProfilePage請求它為止。

class TokenStorage {
    static let shared = TokenStorage()
    public var token: String = ""
}

然后您會收到令牌呼叫:

TokenStorage.shared.token = receivedToken

並在ProfilePage中請求:

print(TokenStorage.shared.token)

當您要導航控制器中不存在的NextVC時,則必須像這樣將數據與類的實例綁定:

let vc = UIStoryboard(name: "ProfilePage", bundle: nil).instantiateInitialViewController() as! ProfilePage
vc. logInToken = "abcdefgh"
self.navigationController?.pushViewController(vc, animated: true)

class ProfilePage: UIViewController {
   var logInToken = "" // you will receive the token in this variable.
}

在您的情況下,似乎ProfilePage不存在。

注意:當您要將值從ProfilePage傳遞到SignInViewController時,Delegate將使用相反的情況。

要么

如果您所有的API都需要令牌,那么您可以在類級別進行聲明或將其保存到UserDefauls:

1)

var logInToken = "" // your variable visible the entire application classes
class ProfilePage: UIViewController {

}

func loginAzure(email: String, password: String) {
    logInToken = "abcdefgh" //Just assign and use it
}

2)

UserDefaults.standard.set("aasdfa", forKey: "token")
let token = UserDefaults.standard.value(forKey: "token"

另外,您正在做不好的編碼,您必須了解OOP的

 let signInVC = SignInViewController() signInVC.delegateToken = self 

這將代表內存中單獨的實例,並且每個對象都有其自己的屬性和行為。

只需使用“准備進行搜索”

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
   if segue.identifier == "ProfileSegue" {
     if let vc = segue.destination as? ProfilePage {
       vc.token = self.token
    }
  }   
}

嘗試使用:

protocol sendTokenDelegate: class {
    func sendToken(login:String)
}
class SignInViewController: UIViewController  {
    weak var delegateToken:sendTokenDelegate?
    func loginAzure(email: String, password: String) {
        token = "abcdefgh"
        if self.delegateToken != nil{
         self.delegateToken?.sendToken(login: token)
        }
    }
}


class ProfilePage: UIViewController, UITableViewDelegate, UITableViewDataSource, sendTokenDelegate {
    override func viewDidLoad() {
        //get your instantiateViewController from storyboard
        let signInVC = self.storyboard?.instantiateViewController(withIdentifier: "SignInViewControllerIdentifire") as! SignInViewController
        signInVC.delegateToken = self
    }
    func sendToken(login: String) {
        self.logInToken = login
        print("Login Token in Profile Page is \(login)")
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM