从UITableViewController转换时,viewDidLoad被调用两次

时间:2014-10-14 22:27:59

标签: ios objective-c memory viewdidload

当我从UITableViewController到常规视图控制器的push segue时,viewDidLoad方法被调用两次。这使得第二个视图控制器上的内容也出现两次。我认为viewDidLoad应该只调用一次。以下是发生这种情况的代码。

viewDidLoad中

-(void)viewDidLoad{
    [super viewDidLoad];

    //Change table view cell height if it is an ipad
    UIDevice *device = [UIDevice currentDevice];
    self.isiPad = (device.userInterfaceIdiom == UIUserInterfaceIdiomPad);

    //Initialize the quiz
    [self initializeTheQuiz];
}

推送Segue

- (IBAction)showQuiz:(id)sender{
    UIButton *senderButton = (UIButton *)sender;

    if (senderButton.tag <= 7 && senderButton.tag >= 0) {
        [self performSegueWithIdentifier:@"showQuiz" sender:sender];
    }else{
        [self performSegueWithIdentifier:@"showFunFacts" sender:sender];
    }
}

以下是我执行push segue的方法

- (IBAction)showQuiz:(id)sender{
   UIButton *senderButton = (UIButton *)sender;

    if (senderButton.tag <= 7 && senderButton.tag >= 0) {
        [self performSegueWithIdentifier:@"showQuiz" sender:sender];
    }else{
        [self performSegueWithIdentifier:@"showFunFacts" sender:sender];
    }
}

//Prepare for segue
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
     UIButton *senderButton = (UIButton *)sender;

    //Set the quiz name based on the button
    //Set the destination view controller
    if ([segue.identifier isEqualToString:@"showQuiz"]) {
        QuizViewController *quizViewController = (QuizViewController *)segue.destinationViewController;

        if (senderButton.tag == 1) {

            quizViewController.quizName = @"North America";

        }else if (senderButton.tag == 2){

            quizViewController.quizName = @"South America";

        }else if (senderButton.tag == 3){

            quizViewController.quizName = @"Africa";

        }else if (senderButton.tag == 4){

            quizViewController.quizName = @"Europe";

        }else if (senderButton.tag == 5){

            quizViewController.quizName = @"Asia";

        }else if (senderButton.tag == 6){

            quizViewController.quizName = @"Oceania";

        }else if (senderButton.tag == 7){

            quizViewController.quizName = @"Antarctica";

        }
    }else if ([segue.identifier isEqualToString:@"showFunFacts"]){
        FunFactsTableViewController *funFactsViewController = (FunFactsTableViewController *)segue.destinationViewController;

        if (senderButton.tag == 8) {

            funFactsViewController.funFactsName = @"North America";

        }else if (senderButton.tag == 9){

            funFactsViewController.funFactsName = @"South America";

        }else if (senderButton.tag == 10){

            funFactsViewController.funFactsName = @"Africa";

        }else if (senderButton.tag == 11){

            funFactsViewController.funFactsName = @"Europe";

        }else if (senderButton.tag == 12){

            funFactsViewController.funFactsName = @"Asia";

        }else if (senderButton.tag == 13){

            funFactsViewController.funFactsName = @"Oceania";

        }else if (senderButton.tag == 14){

            funFactsViewController.funFactsName = @"Antarctica";

        }
    }
}

0 个答案:

没有答案