SDK 8.1在iOS 8.1下运行时会出现此问题,但在iOS 7下运行时则不会出现此问题.仅适用于iPad.问题出现在模拟器和硬件设备上.
下面的代码演示了一个视图控制器,它包含一行UITableView,其中包含一行UIButton.点击按钮或行将导致弹出窗口出现.点击按钮时这很好用,但是当点击表视图的行时,弹出窗口会有一些延迟.在我的测试中,我第一次点击行时弹出窗口通常会出现很少或没有延迟,但是第二次点击该行时,弹出窗口可能需要几秒钟才会出现,并且通常直到我点击其他地方才出现风景.但是,即使在行上的第一次敲击时也会发生延迟(特别是在硬件上测试时).
我展示的代码已经尽可能地降低了,同时保留了问题.在我看来,UITableView在某种程度上是导致这种延迟发生的关键.
我知道在iOS 8中不推荐使用某些UIPopoverController代码.我试图在iOS 8中使用UIPopoverPresentationController,结果是相同的:在弹出窗口出现之前有时会有很长的延迟.我对这种新方法还不是很熟悉,所以我可能会犯错误,但无论如何都可以通过将USE_TRADITIONAL_METHOD宏设置为0而不是1来测试iOS 8代码.
任何有关如何修复或绕过此建议(同时仍使用tableview)的建议将不胜感激.
#import "MyViewController.h"
@interface MyViewController ()
@property (nonatomic) UIPopoverController* myPopoverController;
@end
@implementation MyViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// setup table view
UITableView* tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 500, 500) style:UITableViewStyleGrouped];
[self.view addSubview:tableView];
tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
tableView.dataSource = self;
tableView.delegate = self;
// setup button
UIButton* button = [UIButton buttonWithType:UIButtonTypeSystem];
[self.view addSubview:button];
button.frame = CGRectMake(20, 550, 100, 20);
[button setTitle:@"Show popover" forState:UIControlStateNormal];
[button addTarget:self action:@selector(showPopover:) forControlEvents:UIControlEventTouchUpInside];
}
- (IBAction)showPopover:(id)sender
{
UIView* senderView = (UIView*)sender;
UIViewController* contentViewController = [[UIViewController alloc] init];
contentViewController.view.backgroundColor = [UIColor purpleColor];
#define USE_TRADITIONAL_METHOD 1
#if USE_TRADITIONAL_METHOD
self.myPopoverController = [[UIPopoverController alloc] initWithContentViewController:contentViewController];
[self.myPopoverController presentPopoverFromRect:senderView.frame inView:senderView.superview permittedArrowDirections:UIPopoverArrowDirectionLeft animated:NO];
#else
contentViewController.modalPresentationStyle = UIModalPresentationPopover;
[self presentViewController:contentViewController animated:NO completion:^{
NSLog(@"Present completion"); // As expected, this is executed once the popover is actually displayed (which can take a while)
}];
// do configuration *after* call to present, as explained in Apple documentation:
UIPopoverPresentationController* popoverController = contentViewController.popoverPresentationController;
popoverController.sourceRect = senderView.frame;
popoverController.sourceView = senderView;
popoverController.permittedArrowDirections = UIPopoverArrowDirectionLeft;
#endif
NSLog(@"Popover is visible: %d", self.myPopoverController.isPopoverVisible); // This shows "1" (visible) for USE_TRADITIONAL_METHOD, under both iOS 7 and iOS 8
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 1;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
cell.textLabel.text = @"Show popover";
return cell;
}
#pragma mark - UITableViewDelegate
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self showPopover:[tableView cellForRowAtIndexPath:indexPath]];
}
@end
我发现在尝试显示弹出框之前取消选择该行似乎可以解决问题.这可能是一个有用的解决方法,但我仍然在寻找更好的答案,因为这可能不是很强大.
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:NO]; // adding this line appears to fix the problem
[self showPopover:[tableView cellForRowAtIndexPath:indexPath]];
}