0

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 !

Ashish Kakkad
  • 23,586
  • 12
  • 103
  • 136
user3405644
  • 195
  • 1
  • 2
  • 11

2 Answers2

1

You need To initialize your root view controller with the navigation controller. Here is the code.

In your AppDelegate.h

#import <UIKit/UIKit.h>
#import "HomeViewController.h"
@class HomeViewController;
@interface IDSAppDelegate : UIResponder <UIApplicationDelegate>{
    UINavigationController *nav;
}
@property (strong, nonatomic) HomeViewController *homeViewController;
@end

In your AppDelegate.m

#import "IDSAppDelegate.h"
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.homeViewController = [[HomeViewController alloc] initWithNibName:@"HomeViewController" bundle:nil];
    nav=[[UINavigationController alloc]initWithRootViewController:self.homeViewController];
    self.window.rootViewController = nav;
    [self.window makeKeyAndVisible];  
    return YES;
}
Ashish Kakkad
  • 23,586
  • 12
  • 103
  • 136
1

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 !

user3405644
  • 195
  • 1
  • 2
  • 11