简体   繁体   English

我正在尝试使用协议和委托模式,它将我的数组中的数据传递回父视图 controller

[英]I'm trying to use a protocol and delegate pattern which will pass the data in my array back to the parent view controller

I am new to Xcode and am trying to save an array from secondViewController into the View controller.我是 Xcode 的新手,我正在尝试将数组从 secondViewController 保存到视图 controller 中。 I have aa series of view controllers embedded in a navigation controller so when I click 'back' on the navigation bar I want to keep the data that was collected in an array 'collectionArray' and save to 'collectionArray2'.我在导航 controller 中嵌入了一系列视图控制器,因此当我单击导航栏上的“返回”时,我想保留在数组“collectionArray”中收集的数据并保存到“collectionArray2”。 Here is the protocol delegate method I've tried:这是我尝试过的协议委托方法:

This is in my ViewController where I want the array saved to:这是在我的 ViewController 中,我希望将数组保存到:

import UIKit

class ViewController: UIViewController {
    
    var collectionArray2: [String] = []
    
    override func viewDidLoad() {
        super.viewDidLoad()
        let controller = secondViewController()
        controller.delegate = self
        
    }
    
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        let first = segue.destination as! secondViewController
        first.collectionArray.append(contentsOf: collectionArray2)
    }
}


extension ViewController: PopupDelegate {
    func popupSelectedView(array: [String]) {
        collectionArray2.append(contentsOf: array)
        
    }
}

This is my secondViewController where I want to take 'collectionArray':这是我要使用“collectionArray”的第二个视图控制器:

import UIKit

protocol PopupDelegate: class{

    func popupSelectedView(array: [String])
}

class secondViewController: UIViewController {
    
    var exerciseButton: String!
    var collectionArray: [String] = []
    weak var delegate: PopupDelegate?
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }

    @IBAction func Exercisess(_ sender: UIButton){
        exerciseButton = sender.currentTitle
        collectionArray.append(exerciseButton!)
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        if self.isMovingFromParent {
            delegate?.popupSelectedView(array: collectionArray)
        }
    }
    
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        
        let second = segue.destination as! FinalViewController
        second.cellLabelArray.append(contentsOf: collectionArray)
    }
}

Thank you谢谢

your problem is that your are referring to a difference instance of secondViewController .您的问题是您指的是secondViewController的不同实例。 Don't set your delegate in viewDidLoad but when you prepare the segue:不要在 viewDidLoad 中设置您的委托,但是当您准备 segue 时:

    override func viewDidLoad() {
        super.viewDidLoad()
        //let controller = secondViewController() REMOVE THIS
        //controller.delegate = self REMOVE THIS
        
    }
    
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        let first = segue.destination as! secondViewController
        first.collectionArray.append(contentsOf: collectionArray2)
        first.delegate = self // set the delate here
    }
}

by the way, you should name all your classes starting with capital letters, so it should be SecondViewController顺便说一句,你应该以大写字母开头的所有类命名,所以它应该是SecondViewController

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 问题将数据传递回第一个视图 controller 与委托和协议(Swift) - issue passing data back to first view controller with delegate and protocol (Swift) 如果我有两个实现协议的视图控制器,如何指定我的第三个控制器的委托者? - If I have two view controllers that implements a protocol, how do I specify which my is the delegate for my third controller? 我不能使用委托和协议来传递数据 - I can't use Delegate and Protocol to pass data 使用委托将数据通过多个视图控制器传回 - Use A delegate to pass data back through multiple view controllers 如何使用委托将数据从一个视图控制器传递到另一个视图控制器? - how to use delegate to pass data to from one View Controller to another? 实现协议以将数据从推送的视图控制器传递回视图控制器 - Implementing protocol to pass data back to a view controller from a pushed view controller 如何使用委托将数据从多个视图控制器传递回根视图控制器? IOS 7 - How to pass data back from multiple view controllers to root view controller using delegate? Ios 7 如何使用委托和协议快速编辑和传递单元格数据 - How to edit and pass back cell data with delegate and protocol in swift 我如何将数据传递回父视图控制器。 正在使用addSubView方法显示子视图 - How can i pass data back to parent view controller. am using addSubView method to show child view Swift:我的代理协议方法需要在另一个视图控制器上执行 - Swift: My Delegate Protocol Methods Need to Execute on Another View Controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM