0

When I have a push segue from a UITableViewController to a regular view controller, the viewDidLoad method gets called twice. This makes the content on the second view controller to also appear twice. I thought that viewDidLoad was supposed to only be called once. Here is the code in which this happening.

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];
}

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];
    }
}

Here is how I perform the 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";

        }
    }
}
PoKoBros
  • 701
  • 3
  • 9
  • 25
  • I think there might be a chance that your showQuiz method is actually called twice ? – Nofel Mahmood Oct 14 '14 at 22:30
  • It is a button being pressed so shouldn't it only be called twice if the user presses the button twice?? – PoKoBros Oct 14 '14 at 22:32
  • its an IBAction connected to your button right ? So if it is being pressed twice that means the IBAction gets called twice which means your request to perform segue runs twice ! – Nofel Mahmood Oct 14 '14 at 22:34
  • I just checked by putting a breakpoint in and the show quiz method is only run once. I mistyped in my previous comment. I want the method to only be run once when the user presses the button once. It is not doing this. I think that the error is in my second view controller. – PoKoBros Oct 14 '14 at 22:35
  • Can you put a breakpoint in your `viewDidLoad` to see who's calling it? Normally, it is `loadView` in `UIViewController` who calls it. – Roberto Oct 14 '14 at 22:44
  • Both times it is loadViewIfRequired in UIViewController. – PoKoBros Oct 14 '14 at 22:46
  • Which type of segue are you using? – Acey Oct 14 '14 at 23:53
  • Check how often the init method of your view controller is called and what the backtrace is. – fluidsonic Oct 15 '14 at 04:06
  • The init method is never called. Should it be? – PoKoBros Oct 15 '14 at 11:24

0 Answers0