presentViewController:animated:completionฉันได้รับบางพฤติกรรมแปลกกับ สิ่งที่ฉันกำลังทำคือเกมทายใจ
ฉันมีUIViewController(frequencyViewController) ที่มีUITableView(frequencyTableView) เมื่อผู้ใช้แตะที่แถวที่มีปัญหา TableView ที่มีคำตอบที่ถูกต้องมุมมอง (correctViewController) ควรเป็นอินสแตนซ์และมุมมองควรเลื่อนขึ้นจากด้านล่างของหน้าจอเป็นมุมมองแบบโมดอล สิ่งนี้จะบอกผู้ใช้ว่าพวกเขามีคำตอบที่ถูกต้องและรีเซ็ต frequencyViewController ข้างหลังพร้อมสำหรับคำถามต่อไป correctViewController ถูกปิดเมื่อกดปุ่มเพื่อเปิดเผยคำถามถัดไป
ทั้งหมดนี้ทำงานอย่างถูกต้องทุกครั้งและมุมมองของ correctViewController ปรากฏขึ้นทันทีตราบใดที่มีpresentViewController:animated:completionanimated:NO
หากฉันตั้ง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 ใช้เวลา 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