3
votes

I have an iPad app (XCode 4.6, iOS 6.2, Storyboards, ARC) where I created a UIViewController (not connected to any segue) to be used in a UIPopover. These are the ViewController settings:

enter image description here

enter image description here

I have this code that is supposed to display the "viewForPopover" ViewController from within a UIPopover.

        UIView *anchor = textField;
    UIViewController *viewControllerForPopover =
    [self.storyboard instantiateViewControllerWithIdentifier:@"viewForPopover"];

    popover = [[UIPopoverController alloc]
               initWithContentViewController:viewControllerForPopover];
    [popover presentPopoverFromRect:anchor.frame
                             inView:anchor.superview
           permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];

My problem is: there is no self.storyboard. So how am I supposed to get to the view controller which lies outside of the current class? (current class is a subview of UIView)

2
You shouldn't really be trying to do this from a 'subview of UIView' - you should pass the request to the controlling view controller and it should have access to the storyboard.Wain

2 Answers

8
votes

Call this method on UIStoryboard:

+ (UIStoryboard *)storyboardWithName:(NSString *)name bundle:(NSBundle *)storyboardBundleOrNil

probably like this if your view controller live in 'MainStoryboard.storyboard':

UIViewController *viewControllerForPopover = [[UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil]instantiateViewControllerWithIdentifier:@"viewForPopover"];

0
votes

utility function for swift:

extension UIViewController {


    func presentMyCtl( pushed: Bool){

        let VC = MyController(nibName: "MyController", bundle: nil)

        if pushed{
            let nc = self.navigationController!
            nc.pushViewController(VC, animated: true)

        }else{
            self.present(VC, animated: true, completion: nil)
        }

    }


    func dismissMyCtl(pushed: Bool){

        if pushed{
            let nc = self.navigationController!
            nc.popViewController(animated: true)
        }else{
            self.dismiss(animated: true, completion: nil)
        }
    }
}

where MyController.swift if the swift file for class, and MyController.xib is the file containing the UI.