这是我的代码:
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
时,此对象的数据不可见。
有谁知道为什么会这样,以及如何解决这个问题?
答案 0 :(得分:1)
编辑:我在我的项目中检查了我有类似功能的地方,我将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。就我而言,此方法的工作方式与您在评论中所述的相同。