在Swift中将数据传入和传出Popover

时间:2015-02-03 00:08:54

标签: ios swift

在我当前的项目中,我有详细视图,显示了我的表视图中的特定记录。我有以下标签

@IBOutlet weak var vacationImageView: UIImageView!
@IBOutlet weak var percentSaved: UILabel!

@IBOutlet weak var cost: UILabel!
@IBOutlet weak var saved: UILabel!
@IBOutlet weak var circleProgressView: CircularProgressView!

@IBOutlet weak var daysDepart: UILabel!

我调用一个popover,我想将已保存的当前文本值发送到我的弹出窗口,允许用户编辑它并将其发送回视图。这是我的popover电话。

@IBAction func addPopover(sender: UIView) {
    let savingsInformationViewController = storyboard?.instantiateViewControllerWithIdentifier("SavingsAddPopover") as UIViewController
    savingsInformationViewController.modalPresentationStyle = .Popover
    savingsInformationViewController.preferredContentSize = CGSizeMake(200, 200)



    let popoverController = savingsInformationViewController.popoverPresentationController
    popoverController?.sourceView = sender
    popoverController?.permittedArrowDirections = .Any
    popoverController?.delegate = self




    presentViewController(savingsInformationViewController, animated: true, completion: nil)
}

我原本以为我可以从popover引用数据对象,但不能......至少不是我想的方式。

2 个答案:

答案 0 :(得分:37)

class ViewController: UIViewController,SavingViewControllerDelegate,UIPopoverPresentationControllerDelegate{

        @IBOutlet var labelText: UILabel!
        override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view, typically from a nib.
        }

        @IBAction func buttonPopOverClick(sender: UIButton)
        {
            let savingsInformationViewController = storyboard?.instantiateViewControllerWithIdentifier("SavingsAddPopoverVC") as SavingViewController

            savingsInformationViewController.delegate = self
            savingsInformationViewController.strSaveText=labelText.text

            savingsInformationViewController.modalPresentationStyle = .Popover
            if let popoverController = savingsInformationViewController.popoverPresentationController {
                popoverController.sourceView = sender
                popoverController.sourceRect = sender.bounds
                popoverController.permittedArrowDirections = .Any
                popoverController.delegate = self
            }
            presentViewController(savingsInformationViewController, animated: true, completion: nil)
        }

        func saveText(strText: NSString) {
            labelText.text=strText
        }

        // MARK: - UIPopoverPresentationControllerDelegate
        func adaptivePresentationStyleForPresentationController(controller: UIPresentationController!) -> UIModalPresentationStyle {
            return .FullScreen
        }

        func presentationController(controller: UIPresentationController!, viewControllerForAdaptivePresentationStyle style: UIModalPresentationStyle) -> UIViewController! {
            return UINavigationController(rootViewController: controller.presentedViewController)
        }
    }


    protocol SavingViewControllerDelegate
    {
        func saveText(var strText : NSString)
    }

    class SavingViewController: UIViewController {
        @IBOutlet var textField: UITextField!
        var delegate : SavingViewControllerDelegate?
        var strSaveText : NSString!
        override func viewDidLoad() {
            super.viewDidLoad()
            textField.text = strSaveText
            // Do any additional setup after loading the view.
        }

        @IBAction func buttonDone(sender: UIButton)
        {
            if (self.delegate) != nil
            {
                delegate?.saveText(textField.text)
                self.dismissViewControllerAnimated(true, nil)
            }
        }
    }

答案 1 :(得分:0)

只是指出

func adaptivePresentationStyleForPresentationController(controller: UIPresentationController!) -> UIModalPresentationStyle {
            return .none
        }

在ios 12 / xcode 11上无法正常唤醒,至少对于popover tableview控制器而言

下面的通话有效

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