Swift 从不同的ViewController更改UIButton
我需要从另一个Swift 从不同的ViewController更改UIButton,swift,xcode,uiviewcontroller,uibutton,viewcontroller,Swift,Xcode,Uiviewcontroller,Uibutton,Viewcontroller,我需要从另一个UIViewController 我试过下面的方法 import UIKit class ViewController: UIViewController{ @IBOutlet var B1: UIButton! @IBOutlet var B2: UIButton! override func viewDidLoad() { super.viewDidLoad() } } 输出为: c: V1B1 c: a B1O
UIViewController
我试过下面的方法
import UIKit
class ViewController: UIViewController{
@IBOutlet var B1: UIButton!
@IBOutlet var B2: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
}
}
输出为:
c: V1B1
c: a
B1O: V1B1
B1: Optional("V1B1")
B1: Optional("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()
}
}
输出:
c: V1B1
c: a
B1O: V1B1
B1: Optional("V1B1")
B1: Optional("a")
代码有什么问题?
即使再次加载UIViewController
,“它已更改(如输出所述)!但当视图取消时,它会返回到“V1B1”,这是我放在Main.storyboard中的标题。”
您在新的V1实例中更改了B1的标题,而不是在现有的V1实例中
不要在Disclose方法中为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()
}
}
非常感谢“RajeshKumar R”和“Bhavesh Nayi”
每次按钮都会不同,这就是为什么我认为协议应该是UIButton而不是字符串,我只使用了两个类来解释这个问题,但项目要大得多。@其中一个是否有两个或更多的按钮?另外,如果你能解释我的代码基本上出了什么问题,请谅解,该项目是一个游戏,按钮是关卡,一旦用户完成一个关卡,下一关卡将为他提供“isEnabled=true”@其中一个首先您有一个ViewController类的实例,让我们将其命名为a。然后对View2的一个实例执行segue,我们将其命名为b。现在,当你解雇b时,你回到a。但是您正在为类ViewController创建一个新实例c。如果在c中更改按钮的标题,则不会在a中更改标题
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()
}
}
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()
}
}