ios - swift4:无法将值从当前弹出窗口传递到上一个 View Controller

标签 ios swift uikit swift4

我正在使用 swift4,我将 View Controller 模态地呈现为弹出窗口,并希望在关闭当前弹出窗口时将数据传回之前的 View Controller ,这是第一个 View Controller :

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

这是弹出代码:

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

当我点击弹出窗口关闭按钮时,弹出窗口应该消失并且 SearchResultViewController 中的 errorLable 应该将它的文本更改为新传递的文本,但是什么都没有发生,只有弹出窗口关闭, 我该怎么办?

最佳答案

你委托(delegate)的函数是错误的,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
}

编辑问题后:

您从未将任何实例分配给委托(delegate)变量。 尝试将委托(delegate)变量名称更改为 popoUpDelegate。

pop.delegate 适用于 UIPopoverPresentationControllerDelegate 而不是您的委托(delegate)。这个函数应该是这样的:

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

你的 SearchPopoverViewController 看起来像这样:

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

关于ios - swift4:无法将值从当前弹出窗口传递到上一个 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50719195/

相关文章:

ios - 将 Google Tag Manager 与 Xamarin Google Analytics 组件一起使用会导致错误

c++ - 生成的 Objective-C 接口(interface)中出现编译错误

ios - 在 UITabBarController 上的导航栏中添加按钮并从 View Controller 调用 Segue

Swift - UIAlert 在 UICollectionViewCell 中不起作用

swift - 无法快速占用隐藏 View 空间

Apple 代码中的 iPhone 内存泄漏

ios - 当 UILabel 的高度增长时, View 不会移动

ios - 如何将文本字段键盘更改为仅阿拉伯数字? Swift/UIKit

ios - 长按 UITabBarItem 后禁用切换选项卡

xcode - 核心数据迁移 : Attribute Mapping Value Expression