如何在不使用segue的情况下将Storyboard创建的UIViewController放入UIPopover中?

3
我有一个iPad应用程序(XCode 4.6,iOS 6.2,Storyboards,ARC),其中我创建了一个UIViewController(未连接到任何segue),用于在UIPopover中使用。以下是ViewController的设置: enter image description here enter image description here 我有这段代码,它应该显示“viewForPopover”视图控制器在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];

我的问题是:没有self.storyboard。那么我该如何访问当前类之外的视图控制器呢?(当前类是UIView的子视图)


你不应该试图从“UIView的子视图”中尝试执行此操作-应该将请求传递给控制视图控制器,它应该可以访问Storyboard。 - Wain
2个回答

8

在UIStoryboard上调用此方法:

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

如果您的视图控制器位于'MainStoryboard.storyboard'中,则可能像这样:

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


请注意,本文已被翻译成中文。

1
把 self.storyboard 替换成它在哪里? - SpokaneDude

0

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

MyController.swift是类的Swift文件,而MyController.xib是包含UI的文件。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接