簡體   English   中英

append 將數據放入不同視圖中的數組 swift 中的 controller

[英]append data into an array in different view controller in swift

我現在遇到一個問題,我需要一些幫助。 目前,我的項目中有兩個不同的視圖控制器,VC1 和 VC2。 在項目中,我有一個導航欄和一個標簽欄。

在 VC1 中,我有一個 UIViewController class,它包含一個 UITableView 和 tablaViewCell。 另外,我添加了一組 liveEvents,如下所示。

 public var liveEvents: [LiveEvent] = [
    LiveEvent(title: "aaaa"),
    LiveEvent(title: "bbbbbb"),
    LiveEvent(title: "ccccccc")
]

然后,從單元格中,我將 segue 添加到 VC2。 在 VC2 中,我將 IBaction 添加到 barbutton 項目,如下所示。

@IBOutlet weak var eventTitle: UITextField!

@IBAction func saveNewLiveEvent(_ sender: Any) {
    
    if let eventTitle = eventTitle.text {
        let vc = VC1()
        vc.liveEvents.append(LiveEvent(title: eventTitle))
        print("liveEvent: \(vc.liveEvents)")
        navigationController?.popViewController(animated: true)
    }
}

我試圖 append VC1 中的 liveEvent 數組中的一個事件。

在控制台中,我可以看到 VC2 中附加了新事件,但是,當我在 VC1 中添加打印語句時,我沒有看到新的附加值被附加(而且,新值沒有反映在表上查看。)當我從 VC2 回到 VC1 時。

我想知道如何將 append 值轉換為不同 swift 文件中的數組。

如果你知道我做錯了什么,請告訴我。

您在VC2中創建了一個新的VC1實例,它與您從中推送的實例不同。 所以,這就是你的表沒有更新的原因。

您可以使用delegation方法來實現您的結果。 我為此創建了一個演示代碼,請在下面查看。

TestController1(或 VC1)-

import UIKit

class LiveEvent{
    var title:String
    
    init(title:String) {
        self.title = title
    }
}

class TestController: UIViewController {
    
    @IBOutlet weak var testTableView: UITableView!
    
    public var liveEvents: [LiveEvent] = [
        LiveEvent(title: "aaaa"),
        LiveEvent(title: "bbbbbb"),
        LiveEvent(title: "ccccccc")
    ]
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
}

extension TestController:UITableViewDelegate,UITableViewDataSource{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        liveEvents.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! TestTableCell
        cell.eventLabel.text = liveEvents[indexPath.row].title
        return cell
    }
    
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        let destinationVC = segue.destination as? TestControllerTwo
        destinationVC?.delegate = self
    }
    
}


extension TestController:AddEventsInVC1{
    func addNewEvent(event: LiveEvent) {
        liveEvents.append(event)
        testTableView.reloadData()
    }
}

TestController2(或 VC2)-

import UIKit

protocol AddEventsInVC1:AnyObject {
    func addNewEvent(event:LiveEvent)
}

class TestControllerTwo: UIViewController {
    
    weak var delegate:AddEventsInVC1?
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    
    @IBAction func addEventAndPop(_ sender: Any) {
        
        delegate?.addNewEvent(event: LiveEvent(title: "I am New"))
        navigationController?.popViewController(animated: true)
    }
    
}

CustomCellClass-:

import UIKit

class TestTableCell: UITableViewCell {

    @IBOutlet weak var eventLabel: UILabel!
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}

Storyboard設計很簡單,我只有帶有單個cellTableView ,其中有一個label

我已將 storyboard 本身的seguecell連接到VC2以進行演示。

這是為您提供的解決方案。 簡而言之,您不能像在VC2中那樣創建VC1的新實例。 您需要可以使用protocol方法實現的現有實例。 檢查代碼。 這將解決您的問題。

// 1) Protocol to connect two controllers VC1 and VC2
// You can add protocol code in your VC1 class or you can create new .swift file and add there as well.
protocol LiveEventProtocol: AnyObject {
    func addEvent(_ liveEvent: LiveEvent)
}

class VC1: UIViewController, LiveEventProtocol {
    
    public var liveEvents: [LiveEvent] = [
        LiveEvent(title: "aaaa"),
        LiveEvent(title: "bbbbbb"),
        LiveEvent(title: "ccccccc")
    ]
    
    // 2) go to vc2 but don’t forget to set vc2.delegate = self
    // ⭐️ That is the key here.
    // `vc2.delegate = self` connects VC1 and VC2
    func gotoVC2() {
        let vc2 = VC2()
        vc2.delegate = self
        self.navigationController?.pushViewController(VC2(), animated: true)
    }
    
    // 3) Implementing protocol method.
    // This is a method where VC2 will delegate work to VC1 to append liveEvent.
    func addEvent(_ liveEvent: LiveEvent) {
        self.liveEvents.append(liveEvent)
        // now do your reload stuff.
    }
}

class VC2: ViewController {
    weak var delegate: LiveEventProtocol?
    var eventTitle = UITextField(frame: .zero)
    
    func saveNewLiveEvent() {
        if let eventTitle = eventTitle.text {
            // ❌ this is wrong.
            // Reason,
            //  -> you are creating new instance of VC1
            //  -> Instead we need the existing instance of VC1.
            //  -> This can be achieve using protocol approach.
            // let vc = VC1()
            // vc.liveEvents.append(LiveEvent(title: eventTitle))
            
            // ✅ this is correct way to do it.
            // Here, we are justing passing LiveEvent value through delegate approach which is connected with VC1.
            delegate?.addEvent(LiveEvent(title: eventTitle))
            
            navigationController?.popViewController(animated: true)
        }
    }
}

暫無
暫無

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

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