Search code examples
objective-cuinavigationcontrolleruibuttonuitabbaritempushviewcontroller

Push to UIViewController from a UITabBarItem


I encounter a problem using UITabBarItem and UIButton in my application. My button is inside a UITabBarItem. When I press it I want to be pushed to another controller to display a PDF.

Here is a piece of code that works in other cases :

- (void)viewDidLoad {
   UIImage* imageButton = [UIImage imageNamed:@"pdf-button.png"];
   UIButton *buttonPDF = [UIButton buttonWithType:UIButtonTypeCustom];
   buttonPDF.frame = CGRectMake(SCREEN_WIDTH/2 - 100, 100, 200, 36);
   [buttonPDF setImage:imageButton forState:UIControlStateNormal];
   buttonPDF.contentMode = UIViewContentModeScaleAspectFit;
   buttonPDF.contentHorizontalAlignment = UIControlContentHorizontalAlignmentFill;
   [buttonPDF addTarget:self action:@selector(displayPDFParams:) forControlEvents:UIControlEventTouchUpInside];
   [self.view addSubview:buttonPDF];
}

-(void)displayPDFParams:(UIButton *)sender {
    PDFProduitController *pdfController = [[PDFProduitController alloc] init];
    pdfController.pdf = documentParametres;
    [self.navigationController pushViewController:pdfController animated:YES];
}

displayPDFParams is called but it not push me on my pdfController. I think it's because I'm not able to target the parent navigation controller of my application... Anyone help would be much appreciated. Thanks in advance !


Solution

  • Problem solved by define a property in my UIViewController (as UITabBarItem) like this :

    @property (nonatomic, retain) UINavigationController *superNavController;
    

    And set it in my UITabBarController :

    self.myViewController.superNavController = self.navigationController;
    

    Finally I modified my displayPDFParams method :

    -(void)displayPDFParams:(UIButton *)sender {
    
       PDFProduitController *pdfController = [[PDFProduitController alloc] init];
       pdfController.pdf = self.documentParametres;
    
       [self.superNavController pushViewController:pdfController animated:YES];
    
    }
    

    Works perfectly !