Ios swift4:can';t将值从当前popover传递到上一个视图控制器

Ios swift4:can';t将值从当前popover传递到上一个视图控制器,ios,swift,uikit,swift4,Ios,Swift,Uikit,Swift4,我使用的是swift4,我以popover的形式显示视图控制器,并希望在解除当前popover时将数据传递回上一个视图控制器,这是第一个视图控制器: import UIKit class SearchResultViewController: UIViewController, UIPopoverPresentationControllerDelegate, PopoupDelegate { @IBOutlet var errorLable: UILabel! override

我使用的是
swift4
,我以popover的形式显示视图控制器,并希望在解除当前popover时将数据传递回上一个视图控制器,这是第一个视图控制器:

import UIKit

class SearchResultViewController: UIViewController, UIPopoverPresentationControllerDelegate, PopoupDelegate {

   @IBOutlet var errorLable: UILabel!
   override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if (segue.identifier == "pop") {
        let dest = segue.destination
           if let pop = dest.popoverPresentationController {
               dest.preferredContentSize = CGSize(width: self.view.frame.size.height - 20, height: 500)
               pop.delegate = self
           }
        }
    }

   func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
       return .none
   }

   func popupValueSelected(value: String) {
        print("value: ", value)
        self.errorLable.text = value
   }
}
这是popover代码:

import UIKit

class SearchPopoverViewController: UIViewController {

   @IBOutlet var cityLable: UILabel!
   var delegate: PopoupDelegate?

   @IBAction func closeButtonAction(_ sender: Any) {
       self.delegate?.popupValueSelected(value: "hiiiiii from the other side")
       self.dismiss(animated: false, completion: nil)
   }
}
这是
PopoupDelegate

import Foundation

protocol PopoupDelegate {
   func popupValueSelected(value: String)
}
当我点击popover close按钮时,popover应该关闭,
SearchResultViewController
中的
errorLable
应该将其文本更改为新传递的文本,但没有发生,只有popover关闭,
我该怎么办?

您委托的函数是错误的,
SearchResultViewController中函数内部的函数

func popupValueSelected(value: String) {
   func popupValueSelected(value: String) {
       print("value: ", value)
       self.errorLable.text = value
   }
}
应该是:

func popupValueSelected(value: String) {
    print("value: ", value)
    self.errorLable.text = value
}
编辑问题后:

您从未将任何实例分配给委托变量<代码>尝试将委托变量名称更改为popoUpDelegate。

pop.delegate
用于
UIPopoverPresentationControllerDelegate
而不是您的代表。此函数应如下所示:

if (segue.identifier == "pop") {
     if let dest = segue.destination.popoverPresentationController as? SearchPopoverViewController {
               dest.preferredContentSize = CGSize(width: self.view.frame.size.height - 20, height: 500)
               dest.popoUpDelegate = self
       }
   }
}
class SearchPopoverViewController: UIViewController {

   @IBOutlet var cityLable: UILabel!
   var popoUpDelegate: PopoupDelegate?

   @IBAction func closeButtonAction(_ sender: Any) {
       self.popoUpDelegate?.popupValueSelected(value: "hiiiiii from the other side")
       self.dismiss(animated: false, completion: nil)
   }
}
您的
SearchPopoverViewController
如下所示:

if (segue.identifier == "pop") {
     if let dest = segue.destination.popoverPresentationController as? SearchPopoverViewController {
               dest.preferredContentSize = CGSize(width: self.view.frame.size.height - 20, height: 500)
               dest.popoUpDelegate = self
       }
   }
}
class SearchPopoverViewController: UIViewController {

   @IBOutlet var cityLable: UILabel!
   var popoUpDelegate: PopoupDelegate?

   @IBAction func closeButtonAction(_ sender: Any) {
       self.popoUpDelegate?.popupValueSelected(value: "hiiiiii from the other side")
       self.dismiss(animated: false, completion: nil)
   }
}

您必须定义对SearchResultViewController的委托

var PopoupDelegate : PopoupDelegate?
在pop viewcontroller之前,将委托分配给POPOPOUPDelegate

PopoupDelegate.delegate = self
在函数中有正确的函数。首先要纠正错误。
试试这个。希望这能奏效。当您从
SearchResultViewController
演示
SearchPopoverViewController
时,如果出现任何问题,请告诉我您需要设置委派

使用segue

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  if (segue.identifier == "segueToPopOver") {

       let dest = segue.destination
       if let vc = dest.popoverPresentationController as? SearchPopoverViewController {

           //set other properties

           vc.delegate = self
       }
    }
}
@IBAction func closeButtonAction(_ sender: Any) {
   self.delegate?.popupValueSelected(value: "hiiiiii from the other side")
   self.dismiss(animated: false, completion: nil)
}
使用UINavigationController

let vc = SearchPopoverViewController() // initialise view controller
vc.delegate = self
present(vc, animated: true, completion: nil)
回应代表

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  if (segue.identifier == "segueToPopOver") {

       let dest = segue.destination
       if let vc = dest.popoverPresentationController as? SearchPopoverViewController {

           //set other properties

           vc.delegate = self
       }
    }
}
@IBAction func closeButtonAction(_ sender: Any) {
   self.delegate?.popupValueSelected(value: "hiiiiii from the other side")
   self.dismiss(animated: false, completion: nil)
}
SearchResultViewController
中实现委托方法

func popupValueSelected(value: String) {
    print("value: ", value)
    self.errorLable.text = value
}

我认为您对segues和协议如何为视图控制器工作有一个基本的误解。坦率地说,我会建议在第二节中回顾这个主题。本文通过练习清晰地编写,帮助您了解在视图控制器之间传递信息的基础知识。我去过你所在的地方,虽然这些建议和评论是正确的,但你似乎没有理解他们所说的基础。祝你好运

我使用了popover Disclose委托而不是协议: 此代码解决了我的问题:

搜索结果视图控制器中

func popoverPresentationControllerDidDismissPopover(_ popoverPresentationController: UIPopoverPresentationController) {
    if let searchPopoverViewController =
        popoverPresentationController.presentedViewController as? SearchPopoverViewController{
            self.passed_city_id = searchPopoverViewController.selected_city_id
    }
}
搜索PopOverview控制器中

@IBAction func searchButtonAction(_ sender: Any) {

    self.selected_city_id = 10

    popoverPresentationController?.delegate?.popoverPresentationControllerDidDismissPopover?(popoverPresentationController!)
    self.dismiss(animated: true, completion: nil)
}
您可以使用“展开序列”。请参考

https://medium.com/@mimicatcodes/create-unwind-segues-in-swift-3-8793f7d23c6f

在这一行中self.delegate?.popupValueSelected(值:“另一侧的hiiiiii”)在哪里定义了委托变量?@SharadChauhan抱歉,我忘了复制它,我编辑了帖子,这不是问题检查
SearchPopoverViewController
中的
self.delegate
是否为零?我得到了类型为“PopoupDelegate”的错误值,没有成员“delegate”确定抱歉,请在您的pop viewcontrollers代码之后尝试pop.PopoupDelegate=self。我得到了错误:“SearchPopoverViewController”类型的值没有成员“PopoupDelegate”在SearchPopoverViewController中检查self.delegate是否为
nil
?对不起,我是初学者,我不明白您要在哪里检查它,我将print(self.delegate)设置为invewidLoad我得到了类型为“SearchResultViewController”的错误值没有成员“delegate”,您需要添加
print(self.delegate)
中的
搜索PopOverview控制器
和。检查是否为零。我得到了类型为“SearchResultViewController”的错误值,在SearchPopoverViewController中没有成员“委派”签入