gpt4 book ai didi

ios - Swift 4 - 将数据从 ViewController 传递到 ViewController 到标签栏

转载 作者:行者123 更新时间:2023-11-28 14:48:43 25 4
gpt4 key购买 nike

这是我的代码:

MainViewControler

class LoginViewController: UIViewController {

// outlets
@IBOutlet weak var textFieldLogin: UITextField!
@IBOutlet weak var textFieldPassword: UITextField!
let cms = Connect()
var loggedUser : LoginUser?

@IBAction func btnLoginPressed(_ sender: Any) {
if self.textFieldLogin.text?.isEmpty ?? true || self.textFieldPassword.text?.isEmpty ?? true {
self.errorLoginMessage(txt: "Uzupełnij wszystkie pola!!", title: "Błąd")
} else {
cms.checkUsersLogin(login: self.textFieldLogin.text, password: self.textFieldPassword.text, callback: { (data, error) in
if error == nil{
if let dane = data {
do {
let decoder = JSONDecoder()
self.loggedUser = try decoder.decode(LoginUser.self, from: data!)

if ((self.loggedUser?.id ) == nil) {
let json = try? JSONSerialization.jsonObject(with: data!, options: [])

if let dictionary = json as? [String: Any] {
if let komunikat = dictionary["komunikat"] as? String, let title = dictionary["error"] as? String {
DispatchQueue.main.async {
self.errorLoginMessage(txt: komunikat, title: title)
}
}
} else {
DispatchQueue.main.async {
self.errorLoginMessage(txt: "Podany login lub hasło jest błędny!!", title: "Błąd")
}
}
} else {
DispatchQueue.main.async {
self.performSegue(withIdentifier: "toLoginUser", sender: self)
}
}
}
catch {
print("Error in decoder")
}
} else {
print("Error 103: \(data)")
}
} else {
print("Error 104: \(error)")
}


})
}
}




override func viewDidLoad() {
super.viewDidLoad()

}


override func prepare(for segue: UIStoryboardSegue, sender: Any?){
if segue.identifier == "toLoginUser" {
// access the second tab bar
//let secondDes = barViewControllers.viewControllers?[1] as! SecondViewController
//secondDes.test = "Hello TabBar 2"

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let tabVC = storyboard.instantiateViewController(withIdentifier: "MainVC") as! UITabBarController
self.present(tabVC, animated: true, completion: {
let vc = tabVC.selectedViewController as! NewsViewController
vc.loggedUser = self.loggedUser
})

}
}


}

TabBarViewController:

class NewsViewController: UIViewController {

var loggedUser : LoginUser?

override func viewDidLoad() {
super.viewDidLoad()
print("Sprawdzam obiekt: \(loggedUser)")
dump(loggedUser)
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}


@IBAction func testBtnPressed(_ sender: Any) {
print("Sprawdzam obiekt: \(self.loggedUser)")
dump(self.loggedUser)
}

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.tabBarController?.navigationItem.title = "Aktualności"
}
override var prefersStatusBarHidden: Bool {
return true
}
}

我在 MainViewControler 中创建了正确的对象,但是当我打开 TabbedViewControler 时 - 该对象的数据不可见。

有谁知道为什么会这样以及如何解决这个问题?

最佳答案

编辑:我检查了我的项目,那里有类似的功能,我将 loggedUser 对象传递给下一个 View Controller,我有这样的东西:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "toLoginUser" {
let navCon = segue.destination as! UINavigationController
let newsVC = navCon.topViewController as! NewsViewController
if let user = sender as? NSManagedObject {
newsVC.loggedUser = self.loggedUser
}

}
}

尝试在没有 self.present() 方法的情况下使用它,因为 View 应该会自动呈现。在我的例子中,这个方法就像你在评论中描述的那样工作。

关于ios - Swift 4 - 将数据从 ViewController 传递到 ViewController 到标签栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50019015/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com