使用 XCode 5 的 iPhone 弹出视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19238526/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 04:02:37  来源:igfitidea点击:

Popover view for iPhone using XCode 5

iphoneiosobjective-cxcodefoundation

提问by Sèb

I wanted to reuse the popover for iPhone described in this videowhich is exactly what I need.

我想重用本视频中描述的 iPhone 弹出窗口,这正是我所需要的。

The problem is that I couldn't bind a UIViewControllerproperty to the popover's UIViewControllerlike in the video.

问题是我无法将UIViewController属性绑定到UIViewController视频中的弹出窗口。

One difference with the video is that it has been made using XCode 4.2 and I'm using XCode 5.

与视频的一个区别是它是使用 XCode 4.2 制作的,而我使用的是 XCode 5。

So the question is: How to make a popover for iPhone like in the videoon XCode 5?

所以问题是:如何像XCode 5 上的视频那样为 iPhone 制作一个弹出框?

Here is the XCode 5 project I am struggling with.

这是我正在努力解决的 XCode 5 项目。

回答by Sèb

I figured out a way to get popover to work on iPhone and iPad programmatically !

我想出了一种让 popover 以编程方式在 iPhone 和 iPad 上工作的方法!

  • Create a category to make popover available on iPhone (more details here)

    //UIPopover+Iphone.h
    @interface UIPopoverController (overrides)
    + (BOOL)_popoversDisabled;
    @end
    
    //UIPopover+Iphone.m
    @implementation UIPopoverController (overrides)
    + (BOOL)_popoversDisabled { return NO;
    }
    @end
    
  • Create the button which will show the popover and implement the method it calls

  • 创建一个类别以使 iPhone 上的 popover 可用(更多详细信息在这里

    //UIPopover+Iphone.h
    @interface UIPopoverController (overrides)
    + (BOOL)_popoversDisabled;
    @end
    
    //UIPopover+Iphone.m
    @implementation UIPopoverController (overrides)
    + (BOOL)_popoversDisabled { return NO;
    }
    @end
    
  • 创建将显示弹出窗口的按钮并实现它调用的方法

ExampleUIViewController.h

示例UIViewController.h

@interface ExampleViewController : UIViewController <UIPopoverControllerDelegate>
    @property (strong, nonatomic) UIButton *detailButton;
    @property (nonatomic, retain) IBOutlet UIPopoverController *poc;

UIPopoverController poc has to be held in an instance variable, more details here.

UIPopoverController poc 必须保存在一个实例变量中,更多细节在这里

ExampleUIViewController.m

示例UIViewController.m

- (void)viewDidLoad {
    _detailButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_detailButton addTarget:self
                action:@selector(showPop:)
      forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_detailButton];
}

-(void)showPop:(UIButton *)button {
   UIViewController *detailsViewController = [[DetailsViewController alloc] initWithNibName:@"DetailsViewController" bundle:nil];
   self.poc = [[UIPopoverController alloc] initWithContentViewController:detailsViewController];
   [self.poc setDelegate:self];
   [self.poc presentPopoverFromRect:_detailButton.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionLeft animated:YES];
}
  • Create the UIViewController that will contain what's displayed inside the popover (called DetailsViewController in the example)
  • 创建 UIViewController ,它将包含弹出框内显示的内容(在示例中称为 DetailsViewController)

Simply create it in your project by a right click -> New File -> Objective c class -> UIViewController and tick the box "With XIB".

只需通过右键单击 -> 新建文件 -> Objective c 类 -> UIViewController 在您的项目中创建它,然后勾选“With XIB”框。

Then a popover will appear right next to the button when tapped.

然后点击时按钮旁边会出现一个弹出窗口。

Tested OK on iOs5 and above.

在 iOs5 及更高版本上测试正常。