Answer the question
In order to leave comments, you need to log in
Pass data between controllers programmatically?
There are HomeViewController and NextViewController how to pass a variable from the first to the second without using storyboard and segue (and how back from the second to the first), the variable must be passed before loading viewDidLoad()
The transition is done like this
func handleSearch(){
navigationController?.pushViewController(AlfavitController(), animated: true)
}
Answer the question
In order to leave comments, you need to log in
If you use a storyboard, then you can pass data through prepareForSegue
Let's say you have a NextViewController with a variable name: String
class NextViewController: UIViewController {
var name: String!
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let dest = segue.destination as? NextViewController {
dest.name = "Jessica"
}
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let dest = segue.destination as? NextViewController {
if segue.identifier == "action1" {
dest.name = "Jessica"
} else if segue.identifier == "action2" {
dest.name = "Jessica"
dest.sex = "female"
}
}
}
dest.modalPresentationStyle = .fullScreen
dest.modalTransitionStyle = .coverVertical
present(dest, animated: true, completion: nil)
protocol NextViewControllerDelegate {
func callback(_ someString: String)
}
class NextViewController: UIViewController {
var name: String!
var delegate: NextViewControllerDelegate?
func someMethod() {
delegate?. callback("delegate callback")
}
}
//Тут контроллер из которого открываем CustomViewController, нам помимо name нужно теперь еще задать delegate=self
class HomeViewController: UIViewController, NextViewControllerDelegate {
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let dest = segue.destination as? NextViewController {
dest.name = "Jessica"
dest.delegate = self
}
}
//MARK: NextViewControllerDelegate
func callback(_ someString: String) {
print("got callback with string: \(someString)")
}
}
let kNotifNextViewControllerCallback = NSNotification.Name(rawValue: "kNotifNextViewControllerCallback")
class NextViewController: UIViewController {
var name: String!
func someMethod() {
NotificationCenter.default.post(name: kNotifNextViewControllerCallback, object: "notification text")
}
}
//Тут HomeViewController из которого открываем CustomViewController, мы в нем будем слушать kNotifNextViewControllerCallback в метод gotNotification
class HomeViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(gotNotification(notification:)), name: kNotifNextViewControllerCallback, object: nil)
}
deinit {
NotificationCenter.default.removeObserver(self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let dest = segue.destination as? NextViewController {
dest.name = "Jessica"
}
}
//MARK: Notification observer
func gotNotification(notification: Notification) {
print("got notification with object: \(notification.object)")
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question