gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 05:25:01 27 4
gpt4 key购买 nike

在我当前的项目中,我有一个详细 View ,它显示了我的 TableView 中的特定记录。我有以下标签

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

我调用了一个弹出窗口,我想将 saved 的当前文本值发送到我的弹出窗口,允许用户对其进行编辑并将其发送回 View 。这是我的弹出窗口调用。

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

我原以为我可以从弹出窗口引用数据对象,但不能……至少不是我想的那样。

最佳答案

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

关于ios - 在 Swift 中将数据传入和传出 Popover,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28289173/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com