12

In my current project i have detail view that shows a specific record from my table view. I have the following labels

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

I call a popover that I want to send the current text value of saved to my popup, allow the user to edit it and send it back to the view. Here is my popover call.

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

I would have thought I could reference the data object from the popover but can't..at least not the way I'm thinking.

ddpishere
  • 751
  • 1
  • 8
  • 23
  • Looks like you are trying to make savingsInformationViewController be the content controller. Why not add a property to that class to hold your data and simply assign it after you instantiate the controller in the method you are showing above? Lots of popover examples show the behavior. – Tommie C. Feb 03 '15 at 00:12
  • What do you mean by "can not reference"? How did you try? – qwerty_so Feb 03 '15 at 00:26
  • I tried to reference my variable object vacation.totalSaved which I am using in the view that calls the popover – ddpishere Feb 03 '15 at 00:31
  • @TommieC. Sorry but I am just learning swift..could you point me to a good popover example that does this – ddpishere Feb 03 '15 at 00:34
  • Sure - here is an example showing a couple of approaches including just using a storyboard with a prepare for segue and storyboard which can be used to pass data in the normal way. http://stackoverflow.com/questions/24635744/how-to-present-popover-properly-in-ios-8. It may be easier for you to setup a property of the aforementioned content view controller class; i.e. var data:AnyObject? Then set the property using savingsInformationViewController.data = importantInfo in the method you've defined above. In your delegate methods inside that view controller just send the data to a label et al. – Tommie C. Feb 03 '15 at 00:42
  • You are doing right just use protocol and delegate to send message back to ViewControllers when you are done with UIPopover. Let me know I can make small example if you need :) – Shashi3456643 Feb 03 '15 at 01:02
  • @Shashi3456643. Yes would love to see that. I like to see various ways of doing it and choose one i like the best – ddpishere Feb 03 '15 at 01:03
  • Cool I am on my way to home I will post you example in an hour. Example would be I will send some text value to another viewControllers which will be open in UIPopover there you will see text message in textfield you will be able to edit the textfield and when hit done button it will send edited text message back ViewController from where you came and will show the edited message in in uilabel. – Shashi3456643 Feb 03 '15 at 01:36
  • that will help me immensely – ddpishere Feb 03 '15 at 03:11
  • @ddpishere I have posted example. Please let me know if you get any issue. – Shashi3456643 Feb 03 '15 at 05:15
  • @Shashi3456643 I noticed in the buttonPopOverClick you have instantiate as UINavigationController. I do not have a AddPopover view embedded in a navigation controller. Does it need to be? – ddpishere Feb 03 '15 at 17:08
  • @ddpishere I have updated my post now it should be fine. – Shashi3456643 Feb 03 '15 at 17:19

2 Answers2

38
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)
            }
        }
    }
Cœur
  • 37,241
  • 25
  • 195
  • 267
Shashi3456643
  • 2,021
  • 17
  • 21
0

just to point out

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

not woking properly on ios 12 /xcode 11, at least for popover tableview controller

The call below works

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