iphone - 使用 XCode 5 的 iPhone 弹出窗口 View

标签 iphone ios objective-c xcode foundation

我想重复使用 this video 中描述的 iPhone 弹出窗口这正是我所需要的。

问题是我无法像视频中那样将 UIViewController 属性绑定(bind)到弹出窗口的 UIViewController

视频的一个不同之处在于它是使用 XCode 4.2 制作的,而我使用的是 XCode 5。

所以问题是:如何像 video 中那样为 iPhone 制作弹出窗口在 XCode 5 上?

Here is the XCode 5 project I am struggling with.

最佳答案

我找到了一种让弹出窗口以编程方式在 iPhone 和 iPad 上工作的方法!

  • 创建一个类别以使弹出窗口在 iPhone 上可用(更多详细信息 here)

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

ExampleUIViewController.h

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

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

ExampleUIViewController.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];
}
  • 创建 UIViewController,它将包含弹出框内显示的内容(在示例中称为 DetailsViewController)

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

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

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

关于iphone - 使用 XCode 5 的 iPhone 弹出窗口 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19238526/

相关文章:

iphone - 无法在 UIButton 中使用自定义字体

ios - 从另一个线程通知 UITable

objective-c - 在 Cocoa 中构建服务器/客户端应用程序

ios - 如何在 iOS Swift 中获取实际的日期时间?即使用户从设备设置中更改了日期时间

iphone - UITableview 中的第一行,与其他行不同

iphone - 如何获取我的 iPhone 设备的电话号码?

iphone - QRCode在ios中编码一个简单的字符串

ios - 歌曲不可用时的警报 View

ios - 为什么当 Objective-C 有冲突返回类型时会发生这种情况?

objective-c - 澄清 objective c 对象的一般自定义初始化方法/指针