在我目前的项目中,我有详细的视图,从我的表格视图显示一个特定的记录.我有以下标签
@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中的数据对象,但是至少不是我的想法.
解决方法
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,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) } } }