簡體   English   中英

從不同的ViewController更改UIButton

[英]Changing UIButton From different ViewController

我需要從另一個UIViewController更改UIButton (狀態,標題)

我嘗試了以下

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.storyboardMain.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")

代碼有什么問題?

即使UIViewController再次加載,如何永久更改UIButtons

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()
    }
}

“它已更改(如輸出所示)!但是當視圖消失時,它返回到“ 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()
    }
}

非常感謝“ 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()
    }
}


暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM