我正在使用 presentViewController:animated:completion
时遇到一些奇怪的行为。我正在制作一个猜谜游戏。
我有一个包含 UITableView
(frequencyTableView) 的 UIViewController
(frequencyViewController)。当用户点击包含正确答案的 questionTableView 行时,应该实例化一个视图(correctViewController),并将其视图从屏幕底部向上滑动,作为模态视图。这告诉用户他们有一个正确的答案,并准备好下一个问题的 frequencyViewController。按下按钮来解除 correctViewController,以显示下一个问题。
每次都能正常工作,只要 presentViewController:animated:completion
具有 animated:NO
,correctViewController 的视图就会立即出现。
如果我设置 animated:YES
,correctViewController 就会被初始化并调用 viewDidLoad
。但是,viewWillAppear
, viewDidAppear
和 presentViewController:animated:completion
的完成块不会被调用。应用程序仅仅仍然显示 frequencyViewController,直到我进行第二次轻点。现在,viewWillAppear,viewDidAppear 和完成块被调用。
我进行了更深入的调查,发现它不仅是另一个轻点会导致它继续。似乎如果我倾斜或晃动我的 iPhone,这也会导致它触发 viewWillLoad 等。就像它在等待任何其他用户输入一样,然后才会继续。这发生在真正的 iPhone 和模拟器上,我通过向模拟器发送摇动命令证明了这一点。
我真的不知道该怎么做...如果有人能提供任何帮助,我将不胜感激。
谢谢
这是我的代码。很简单...
这是 questionViewController 中作为 questionTableView 委托的代码
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row != [self.frequencyModel currentFrequencyIndex])
{
// If guess was wrong, then mark the selection as incorrect
NSLog(@"Incorrect Guess: %@", [self.frequencyModel frequencyLabelAtIndex:(int)indexPath.row]);
UITableViewCell *cell = [self.frequencyTableView cellForRowAtIndexPath:indexPath];
[cell setBackgroundColor:[UIColor colorWithRed:240/255.0f green:110/255.0f blue:103/255.0f alpha:1.0f]];
}
else
{
// If guess was correct, show correct view
NSLog(@"Correct Guess: %@", [self.frequencyModel frequencyLabelAtIndex:(int)indexPath.row]);
self.correctViewController = [[HFBCorrectViewController alloc] init];
self.correctViewController.delegate = self;
[self presentViewController:self.correctViewController animated:YES completion:^(void){
NSLog(@"Completed Presenting correctViewController");
[self setUpViewForNextQuestion];
}];
}
}
这是 correctViewController 的全部内容。
@implementation HFBCorrectViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self)
{
// Custom initialization
NSLog(@"[HFBCorrectViewController initWithNibName:bundle:]");
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
NSLog(@"[HFBCorrectViewController viewDidLoad]");
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
NSLog(@"[HFBCorrectViewController viewDidAppear]");
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)close:(id)sender
{
NSLog(@"[HFBCorrectViewController close:sender:]");
[self.delegate didDismissCorrectViewController];
}
@end
编辑:
我之前发现了这个问题:UITableView和presentViewController需要点击两次才能显示
如果我将我的didSelectRow
代码更改为以下内容,它每次都能以动画效果正常工作...但它很混乱,并且不清楚为什么一开始不能正常工作。因此,我不认为这是一个答案...
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row != [self.frequencyModel currentFrequencyIndex])
{
// If guess was wrong, then mark the selection as incorrect
NSLog(@"Incorrect Guess: %@", [self.frequencyModel frequencyLabelAtIndex:(int)indexPath.row]);
UITableViewCell *cell = [self.frequencyTableView cellForRowAtIndexPath:indexPath];
[cell setBackgroundColor:[UIColor colorWithRed:240/255.0f green:110/255.0f blue:103/255.0f alpha:1.0f]];
// [cell setAccessoryType:(UITableViewCellAccessoryType)]
}
else
{
// If guess was correct, show correct view
NSLog(@"Correct Guess: %@", [self.frequencyModel frequencyLabelAtIndex:(int)indexPath.row]);
////////////////////////////
// BELOW HERE ARE THE CHANGES
[self performSelector:@selector(showCorrectViewController:) withObject:nil afterDelay:0];
}
}
-(void)showCorrectViewController:(id)sender
{
self.correctViewController = [[HFBCorrectViewController alloc] init];
self.correctViewController.delegate = self;
self.correctViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentViewController:self.correctViewController animated:YES completion:^(void){
NSLog(@"Completed Presenting correctViewController");
[self setUpViewForNextQuestion];
}];
}
presentViewController:
应该触发的动画开始时有很大的延迟。这似乎是iOS 7中的一个bug,也在Apple开发者论坛中讨论过。 - Theo