swift4:无法从当前弹出窗口

时间:2018-06-06 11:22:28

标签: ios swift uikit swift4

我使用swift4,我将视图控制器模态化为popover,并希望在解除当前弹出窗口时将数据传递回上一个视图控制器,这是第一个View控制器:

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

当我点击弹出窗口关闭按钮时,弹出窗口应该被忽略,errorLable中的SearchResultViewController应该将其文本更改为新传递的文本,但没有一个发生,只有popover dismiss , 我该怎么办?

6 个答案:

答案 0 :(得分:1)

您委托函数错误,函数在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
}

编辑问题后:

您从未将任何实例分配给委托变量。 Try to change delegate variable name to 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
       }
   }
}

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

答案 1 :(得分:0)

你必须为SearchResultViewController定义委托。

var PopoupDelegate : PopoupDelegate?

并且在pop viewcontroller之前将委托分配给popoupDelegate

PopoupDelegate.delegate = self

你有正确的功能内部功能。首先要做到正确。 试试这个。希望这会奏效。如果有任何疑虑,请告诉我

答案 2 :(得分:0)

当您从SearchPopoverViewController展示SearchResultViewController时,您需要设置委托。

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

使用UINavigationController

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

回应代表

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

答案 3 :(得分:0)

我认为你对视图控制器的segues和协议如何工作存在根本的误解。坦率地说,我建议在Apple's App Development with Swift中审核该主题。这是用练习清楚地写的,以帮助您理解在视图控制器之间传递信息的基础知识。我一直在你的位置,虽然建议和评论是正确的,但你似乎没有基础来理解他们在说什么。祝你好运!

答案 4 :(得分:0)

我使用popover dismiss委托代替协议: 这段代码解决了我的问题:

SearchResultViewController中的

func popoverPresentationControllerDidDismissPopover(_ popoverPresentationController: UIPopoverPresentationController) {
    if let searchPopoverViewController =
        popoverPresentationController.presentedViewController as? SearchPopoverViewController{
            self.passed_city_id = searchPopoverViewController.selected_city_id
    }
}
SearchPopoverViewController

中的

@IBAction func searchButtonAction(_ sender: Any) {

    self.selected_city_id = 10

    popoverPresentationController?.delegate?.popoverPresentationControllerDidDismissPopover?(popoverPresentationController!)
    self.dismiss(animated: true, completion: nil)
}

答案 5 :(得分:0)

您可以使用“ Unwind segue”。请参阅

windows7_64, mingw32, gcc 6.3.0, eclipse