我的出现了一些奇怪的行为presentViewController:animated:completion
。我要做的本质上是一个猜谜游戏。
我有一个UIViewController
包含UITableView
(frequencyTableView)的(frequencyViewController)。当用户点击questionTableView中包含正确答案的行时,应实例化一个视图(correctViewController),并且其视图应从屏幕底部作为模式视图向上滑动。这告诉用户他们的答案正确,并重置其后面的frequencyViewController以准备下一个问题。按下按钮以显示下一个问题时,会关闭correctViewController。
每次都可以正常工作,只要presentViewController:animated:completion
有has,即可立即显示正确的ViewController的视图animated:NO
。
如果设置animated:YES
,则会初始化correctViewController并调用viewDidLoad
。但是viewWillAppear
,不会调用viewDidAppear
和的from的完成块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];
}];
}
}
这是正确的ViewController的整体
@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需要2次点击才能显示
而且,如果我将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中的错误,Apple Dev论坛中也对此进行了讨论。