Search code examples
iosuiviewcontrolleruipopovercontroller

Pass variable from a ViewController to a UIPopoverControllerView iOS


I am having problems passing a variable from a view controller to a UIPopoverViewcontroller

is the logic not the same as passing data between two usual viewControllers?

i.e setting a variable in view1 & view2 and synthesising then presenting the PopoverView pass the variable from view1 to view2?

I am really struggling with this I have read a little about protocols but seems overkill if it would work like a usual view controller?

An example

    self.optionsPopover.delegate = self;
    popOverViewController = [[PopOverViewController alloc]initWithNibName:@"PopOverViewController" bundle:nil];
    self.optionsPopover = [[UIPopoverController alloc]initWithContentViewController:popOverViewController];

    //dictionary to pass from view1 to view2 which is a popover
    self.popOverViewController.statsDict = self.statsDict;
    [self.optionsPopover setPopoverContentSize:CGSizeMake(320, 480)];
    [self.optionsPopover presentPopoverFromBarButtonItem:optionsButton permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];

Solution

  • There is nothing wrong in your code except you are setting delegate of popOver before creating popover. I have changed the order. And check your dictionary is nil or not

    self.popOverViewController = [[PopOverViewController alloc]initWithNibName:@"PopOverViewController" bundle:nil];
    
     //dictionary to pass from view1 to view2 which is a popover
    //self.popOverViewController.statsDict = self.statsDict;
    
     self.popOverViewController.testString = @"testing"; // This is for testing only
    
     self.optionsPopover = [[UIPopoverController alloc]initWithContentViewController:popOverViewController];
     self.optionsPopover.delegate = self;
    
    [self.optionsPopover setPopoverContentSize:CGSizeMake(320, 480)];
    [self.optionsPopover presentPopoverFromBarButtonItem:optionsButton permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];