我需要从另一个UIButton
更改UIViewController
(状态,标题)
我尝试了以下
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()
}
}
输出为:
c: V1B1
c: a
它已更改(如输出所示)!但是当视图消失时,它会返回到“ V1B1”,这是我在Main.storyboard
中输入的标题
我还尝试使用协议和委托对其进行更改
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()
}
}
输出:
B1O: V1B1
B1: Optional("V1B1")
B1: Optional("a")
代码有什么问题?
即使再次加载UIButtons
,如何永久更改UIViewController
答案 0 :(得分:1)
import UIKit
class ViewController: UIViewController{
@IBOutlet var B1: UIButton!
@IBOutlet var B2: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
}
open override func viewWillAppear(_ animated: Bool) {
// Register to receive notification
NotificationCenter.default.addObserver(self, selector: #selector(self.updateTitle), name: NSNotification.Name(rawValue: "buttonTitleUpdate"), object: nil)
super.viewWillAppear(animated)
}
@objc func updateTitle() -> Void {
print("c: ",B1.currentTitle ?? "")
B1.setTitle("a", for: .normal)
print("c: ",B1.currentTitle ?? "")
}
}
import Foundation
import UIKit
class View2: UIViewController {
@IBAction func Dismiss(_ sender: Any) {
// Post a notification
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "buttonTitleUpdate"), object: nil)
dismiss(animated: true, completion: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
}
}
答案 1 :(得分:0)
“已更改(如输出所示)!但是当视图消失时,它又回到了“ V1B1”,这是我在Main.storyboard中输入的标题”
您已经在V1的新实例中更改了B1的标题,而不是在V1的现有实例中更改了标题。
不要在dismiss方法中为ViewController类创建新实例
class ViewController: UIViewController,TestDelegate {
func change(text: String) {
B1.setTitle(text, 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()
}
}
protocol TestDelegate {
func change(text: String)
}
class View2: UIViewController {
var d: TestDelegate?
@IBAction func Dismiss(_ sender: Any) {
d?.change(text:"NewTitle")
dismiss(animated: true, completion: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
}
}
答案 2 :(得分:0)
非常感谢“ RajeshKumar R”和“ Bhavesh Nayi”
import UIKit
class ViewController: UIViewController,TestDelegate {
func t(NewT: Int) {
let TempButton = self.view.viewWithTag(NewT) as! UIButton
TempButton.setTitle("X", for: .normal)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let dd = segue.destination as? View2 {
dd.d = self
}
}
@IBOutlet var B1: UIButton!
@IBOutlet var B2: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
}
}
import Foundation
import UIKit
protocol TestDelegate {
func t(NewT: Int)
}
class View2: UIViewController {
var d: TestDelegate?
@IBAction func Dismiss(_ sender: Any) {
//just pass the tag
d?.t(NewT: 1)
dismiss(animated: true, completion: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
}
}