0

我已经实现了一个自定义故事板 segue,它在 iOS 6 下运行良好,但在 iOS 5.1 下的模拟器中没有任何反应。问题是在iOS 5.1下popoverController总是nil?!

@implementation PopoverFromRectSegue  

-(id)initWithIdentifier:(NSString *)identifier
             source:(UIViewController *)source
        destination:(UIViewController *)destination {

  if(self = [super initWithIdentifier:identifier
                             source:source
                        destination:destination]) {
  }

  return self;
}

- (void)perform {
    UIPopoverController *popCtrl = ((UIStoryboardPopoverSegue *)self).popoverController;

    id controller = [self sourceViewController];
    if ([controller isKindOfClass:[UIViewController class]] && [controller respondsToSelector:@selector(popoverRect)]) {
    [popCtrl presentPopoverFromRect:[[controller performSelector:@selector(popoverRect)] CGRectValue] inView:[controller view] permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
  }
}
@end

任何帮助/提示表示赞赏。

编辑:刚刚制作了新的示例项目。似乎在 iOS5.1 下没有为自定义 UIStoryBoardSegues 设置 popoverController。我还可以做些什么。正常的弹出框需要一个锚点,但不接受原型 tableView 单元格(编译期间失败),我找不到修改弹出框所在的矩形的方法。

4

1 回答 1

0

这是我的解决方法。
PopoverFromRectSegue.h

@interface PopoverFromRectSegue : UIStoryboardPopoverSegue
  @property (strong, nonatomic) UIPopoverController *popoverCtrl;
@end

PopoverRectFromSegue.m

#import "PopoverFromRectSegue.h"
@implementation PopoverFromRectSegue

- (void)perform
{
  UIPopoverController *popCtrl = ((UIStoryboardPopoverSegue *)self).popoverController;

  // under iOS 5.1 the popoverController iVar is not set
  // so we have to use our own one
  if (nil == popCtrl) {
    popCtrl = self.popoverCtrl;
  }

  id controller = [self sourceViewController];
  if ([controller isKindOfClass:[UIViewController class]] && [controller respondsToSelector:@selector(popoverRect)]) {
    [popCtrl presentPopoverFromRect:[[controller performSelector:@selector(popoverRect)] CGRectValue] inView:[controller view] permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
  }
}

@end

在我的 prepareForSegue 方法中,我执行以下操作。

    if (isPad) {
      self.popoverCtrl = [(UIStoryboardPopoverSegue *)segue popoverController];
      if (nil == _popoverCtrl) {
        self.popoverCtrl = [[UIPopoverController alloc] initWithContentViewController:[segue destinationViewController]];
        ((PopoverFromRectSegue *)segue).popoverCtrl = _popoverCtrl;
      }
      self.popoverRect = [NSValue valueWithCGRect:[self.myTableView rectForRowAtIndexPath:indexPath]];
  }
于 2012-12-12T11:04:57.230 回答