hydev
hydev

Reputation: 744

UIPopover and passing data - getting closer

I have set myself a simple challenge, which I see in many apps and I'm having a lot of difficulty with it.

My task is to have one ViewController with a Navigation bar, button and a label. When you press the button a UIPopover pops up with a new ViewController and the user presses a button inside that popover. When they press that button my ViewController can use that information for something. Thats it. Simple?

Therefore my parent ViewController is set up as:

UIViewController popOverViewController = new PopOverViewController ();
PopoverDelegate _popoverDelegate;  

public override void ViewDidLoad () {
    base.ViewDidLoad ();
    BarButtonItemPopover = new UIPopoverController (popOverViewController);
    BarButtonItemPopover.PopoverContentSize = new SizeF (320, 320);
    _popoverDelegate = new PopoverDelegate();
    BarButtonItemPopover.Delegate = _popoverDelegate;
}

public class PopoverDelegate : UIPopoverControllerDelegate {
     public PopoverDelegate() { }

     public override void DidDismiss (UIPopoverController controller) {
           string mike = "Dismissed";
     }

     public void updateSomething(string someData) {
           // When the user clicks in my popOverViewController I want to come
           // here and do something.  I will then dismiss the popover here.
           string mike = someData;
     }
}

My popOverViewController is set up as UIViewController, with a button on it called btnClickMe and the key bit is this:

 public override void ViewDidLoad () {
      base.ViewDidLoad ();
      this.btnClickMe.TouchUpInside += (s, e) => {
            Console.WriteLine("");
             // When I'm clicked I want to send some 
             // data to the updateSomething("hello world") method
             // in the ViewController that crated me! Help!
        };
 }

I have debugged and when I dismiss the Popover in the simulator the Delegate fires and string mike in my calling ViewController does equal "Dismissed".

What I simply cannot understand or find out is how you get the updateSomething() method to be called/referenced/fired from the popoverViewController. I have looked at numerous examples of people using UIPopover but no-one seems to have an example of doing this in Monotouch there all in Objective C. Does this mean it's not possible? Or is it for very experience people only?

If I'm honest I thought it would be easier in Monotouch than Objective C as I am an experienced C# developer but at the moment I think it would be easier to learn Objective C!!

I hope someone can help me as I'm getting very confused/frustrated.

Thanks

Mike

Upvotes: 0

Views: 517

Answers (1)

jonathanpeppers
jonathanpeppers

Reputation: 26495

There are many ways to solve this problem.

Here is the simplest:

  • Add a property to your PopoverViewController that contains your PopoverDelegate
  • From your button click handler, call YourNewProperty.updateSomething("peppers")

To solve problems like these in general, I use a global "messenger" class. This allows other controllers, delegates, classes, etc. to subscribe to events in a global way and need to necessarily know where they came from. It really helps with this crazy version of MVC Apple came up with.

I use the one from TinyIoC, but it has a bit of a learning curve to set up: https://github.com/grumpydev/TinyIoC/blob/master/src/TinyIoC/TinyMessenger.cs

Upvotes: 1

Related Questions