Вот мой код:
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
- данные этого объекта не видны.
Кто-нибудь знает, почему это происходит и какрешить эту проблему?