i need to change UIButton
(status, title) from another UIViewController
i tried the below
import UIKit
class ViewController: UIViewController{
@IBOutlet var B1: UIButton!
@IBOutlet var B2: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
}
}
import Foundation
import UIKit
class View2: UIViewController {
@IBAction func Dismiss(_ sender: Any) {
h()
dismiss(animated: true, completion: nil)
}
func h(){
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "VC") as? ViewController
vc?.loadViewIfNeeded()
print("c: ",vc?.B1.currentTitle ?? "")
vc?.B1.setTitle("a", for: .normal)
print("c: ",vc?.B1.currentTitle ?? "")
}
override func viewDidLoad() {
super.viewDidLoad()
}
}
The Output is :
c: V1B1
c: a
it's changed (as the output said) ! but when the view dismissed it goes back to "V1B1" which is the title i put in Main.storyboard
i also tried to change it with protocol and delegate
import UIKit
class ViewController: UIViewController,TestDelegate {
func t(NewT: UIButton) {
NewT.setTitle("a", for: .normal)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let dd = segue.destination as? View2 {
dd.d = self
print("B1O: ",B1.currentTitle!)
}
}
@IBOutlet var B1: UIButton!
@IBOutlet var B2: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
}
}
import Foundation
import UIKit
protocol TestDelegate {
func t(NewT: UIButton)
}
class View2: UIViewController {
var d: TestDelegate?
@IBAction func Dismiss(_ sender: Any) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "VC") as? ViewController
vc?.loadViewIfNeeded()
print("B1: ",vc?.B1.currentTitle!)
d?.t(NewT: (vc?.B1!)!)
print("B1: ",vc?.B1.currentTitle!)
dismiss(animated: true, completion: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
}
}
Output:
B1O: V1B1
B1: Optional("V1B1")
B1: Optional("a")
what's wrong with the code ?
How can i change the UIButtons
permanently even if the UIViewController
loaded again