控制器之间的代表不起作用。为什么?

时间:2018-04-06 13:19:40

标签: ios swift delegation

我尝试使用委托将详细信息2(textField)中的ViewController数据发送到ViewController中的数组。 我在这里使用print方法和第一次打印显示一个元素已添加到数组中,但第二个打印方法在ViewVillAppear()下方显示该数组为空。怎么样?我希望能够使用委托将数据添加到我的表中。

["sdsd"]首先从控制台打印 []从控制台再次打印

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

@IBOutlet var add: UIBarButtonItem!
@IBOutlet var tv: UITableView!
var array :[String] = []

override func viewDidLoad() {
    super.viewDidLoad()

    tv.register(UITableViewCell.self, forCellReuseIdentifier: "cell") 
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "segue" {
        let vc: Detail2 = segue.destination as! Detail2
        vc.delegate = self
    }
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return array.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
    cell.textLabel?.text = array[indexPath.row]
    return cell
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
    if editingStyle == .delete {
        array.remove(at: indexPath.row )
        tv.reloadData()
    }
}

func alert () {
}



 override func viewWillAppear(_ animated: Bool) {
        tv.reloadData()
        print(array)

    }

}

extension ViewController: Data {

    func tekst(data: String) {
        array.append(data)
        print(array)

    }   
}

详情2

    protocol Data {
    func tekst (data: String)
}

class Detail2: UIViewController {

    var delegate: Data? = nil
    @IBAction func btn(_ sender: Any) {

        let sb  = storyboard?.instantiateViewController(withIdentifier: "Main" ) as! ViewController
        navigationController?.pushViewController(sb, animated: true)

        if delegate != nil {
            if txtfield.text != nil {

                let napis = txtfield.text
                delegate?.tekst(data: napis!)   
            }   
        }   
    }
    @IBOutlet var btn: UIButton!
    @IBOutlet var txtfield: UITextField!

    override func viewDidLoad() {
        super.viewDidLoad()

        btn.backgroundColor = UIColor.blue
        btn.tintColor = UIColor.white
        btn.layer.cornerRadius = 25
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

4 个答案:

答案 0 :(得分:0)

只需更新您的功能:

@IBAction func btn(_ sender: Any) {
        if delegate != nil {
        if txtfield.text != nil {
            let napis = txtfield.text
            delegate?.tekst(data: napis!)
        }

    navigationController?.popViewController(animated: true)

    }
}

<强>更新

extension ViewController: Data {

    func tekst(data: String) {
        array.append(data)
        print(array)
        self.tv.reloadData()

    }   
}

答案 1 :(得分:0)

您需要在完成处理程序中添加此delegate?.tekst(data: napis!),因为您使用的是navigationController,因此没有完成处理程序的选项,因此必须添加UINavigationController扩展名:

extension UINavigationController {

    public func pushViewController(viewController: UIViewController,
                                   animated: Bool,
                                   completion:  (() -> Void)?) {
        CATransaction.begin()
        CATransaction.setCompletionBlock(completion)
        pushViewController(viewController, animated: animated)
        CATransaction.commit()
    }

}

更改此

navigationController?.pushViewController(sb, animated: true){

    if delegate != nil {
        if txtfield.text != nil {

            let napis = txtfield.text
            delegate?.tekst(data: napis!)

        }

答案 2 :(得分:0)

在Detail2 viewcontroller中更新您的代码

@IBAction func btn(_ sender: Any) {

       if delegate != nil {
           if txtfield.text != nil {
             let napis = txtfield.text
             delegate?.tekst(data: napis!)
            }
        }
        navigationController?.popViewController(animated: true)
}

答案 3 :(得分:0)

ViewController实现委托方法

 func tekst (data: String) { 
    array.append(data)
 }

//详细

@IBAction func btn(_ sender: Any) {

    if txtfield.text != nil {

        let napis = txtfield.text
        delegate?.tekst(data: napis!)

    }

     /// dismiss detail here  don't push main again

     self.navigationController?.popViewController(animated: true)

}