Get Navigation Controller from Tabbar Controller - ios

I am trying to achieve to two things right now:
1) Have a tabbar controller inside a navigation controller
2) Depending on the selected tab, some Navigation buttons change, and I want to push some views into the navigation controller, while staying inside the currently selected tab.
Right now, I push the Navigation Controller like this:
GenericAddViewController *vc = [[GenericAddViewController alloc]init];
UINavigationController *navigationController = [[UINavigationController alloc]initWithRootViewController:vc];
navigationController.modalPresentationStyle = UIModalPresentationFullScreen;
[self presentViewController:navigationController animated:YES completion: nil];
Then, inside the GenericAddViewController.m I have:
#interface GenericAddViewController ()
#property (strong, nonatomic) UITextField *titleTextField;
#property (nonatomic, strong) UITabBarController *tabBarController;
#end
#implementation GenericAddViewController
-(void)loadView
{
[super loadView];
TabOneController *tabOneController = [[TabOneController alloc]init];
tabOneController.title = #"Steps";
TabTwoController *tabTwoController = [[TabTwoController alloc]init];
tabTwoController.title = #"More Information";
NSArray *controllers = [NSArray arrayWithObjects:tabOneController, tabTwoController, nil];
self.tabBarController = [[UITabBarController alloc]init];
self.tabBarController.view.frame = CGRectMake(0, 0, 768, 1004);
self.tabBarController.viewControllers = controllers;
self.tabBarController.delegate = self;
}
This works. The only problem is for example in the TabOneController, I can't do any of these:
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(isDone:)];
[self.navigationItem setRightBarButtonItem:doneButton animated:NO];
Nor
[self.navigationController push...]
Shouldn't be that complicated no?!
Thanks a lot!

Related

Right Button won't show in custom Navigation Controller

My app is a tab bar app with many navigation controllers.
I want these navigation controllers to handle login/logout actions with a custom right bar button. So I've set up my tab bar in AppDelegate this way :
MyFirstViewController *firstViewController = [[MyFirstViewController alloc] init];
UIViewController *firstNavigationController = [[CustomNavigationController alloc]
initWithRootViewController:firstViewController];
MySecondViewController *secondViewController = [[MySecondViewController alloc] init];
UIViewController *secondNavigationController = [[CustomNavigationController alloc]
secondViewController]
initWithRootViewController:secondViewController];
....
[tabBarController setViewControllers:#[firstNavigationController, secondNavigationController]];
Then CustomNavigationController :
#implementation ConnectionNavigationController
- (void) viewDidLoad
{
[self displayConnectionButton];
}
- (void) displayConnectionButton
{
UIImage *portraitImage, *landscapeImage;
portraitImage = [UIImage imageNamed:#"conn.png"];
landscapeImage = [UIImage imageNamed:#"connLandscape.png"];
UIBarButtonItem *connButton = [[UIBarButtonItem alloc]
initWithImage:[portraitImage
imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]
landscapeImagePhone:[landscapeImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]
style:UIBarButtonItemStylePlain target:self action:#selector(showConnectionPopup)];
self.navigationItem.rightBarButtonItem = connButton;
}
#end
I tried with viewDidLoad, viewDidAppear, viewWillAppear but nothing works, I can't get this button to show in the navigation bar. I also tried to add reloadInputViews or setNeedsDisplay. What should I do?
Thanks for your help
Edit : interface of CustomNavigationController
#interface ConnectionNavigationController : UINavigationController
- (void) displayConnectionButton;
#end
From what I understand you should add the displayConnectionButton method to the relevant UIViewController subclass and not to your UINavigationController subclass as this only contains the stack of view controllers.
E.g.
#implementation MyFirstViewController
- (void) viewDidLoad
{
[self displayConnectionButton];
}
- (void) displayConnectionButton
{
UIImage *portraitImage, *landscapeImage;
portraitImage = [UIImage imageNamed:#"conn.png"];
landscapeImage = [UIImage imageNamed:#"connLandscape.png"];
UIBarButtonItem *connButton = [[UIBarButtonItem alloc]
initWithImage:[portraitImage
imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]
landscapeImagePhone:[landscapeImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]
style:UIBarButtonItemStylePlain target:self action:#selector(showConnectionPopup)];
self.navigationItem.rightBarButtonItem = connButton;
}
#end
As a note if you put a breakpoint on your displayConnectionButton in your CustomNavigationController and po self.viewControllers I think you will get an empty array (i.e. no view controllers)

UITabBarController is lost when set a new ViewController

I have the following TabBarController with the uiTabBarItems
[Item1, Item2, Item3, Item4]
This struct works fine (Every ViewController is displayed correctly), my problem is:
When I change to another ViewController from Item1, Item2 ... the TabBarItems bottom are hidden/lose
[, , , ]
I'm using the following code to change of viewController from Item1 ViewController
NewViewController *controller = [[NewViewController alloc]init];
[self.tabBarController setViewControllers:[[NSArray alloc] initWithObjects:controller, nil]];
is correct change of viewcontroller with the code showed previously?
EDIT.-
Basically I want to navigate on ViewControllers of Item1 (UITabBar) without lost the UITabBarItems
If you want to set the active tab, you should't use setViewControllers: since that replaces all your tabs. You should use setSelectedIndex: on your UITabBarController instead.
Try this.
call this method , where to present UITabBar
In .h,
#property (strong, nonatomic) UINavigationController *navigation;
#property(nonatomic, strong) UITabBarController *tabbarcontroller;
In .m,
-(void)loadtabview
{
self.tabbarcontroller = [[UITabBarController alloc] init];
NSMutableArray *localControllersArray = [[NSMutableArray alloc] initWithCapacity:4];
self.firstViewController = [[FirstViewController alloc]initWithNibName:#"firstViewController" bundle:nil];
navigation = [[UINavigationController alloc] initWithRootViewController:self.firstViewController];
self.viewController.navigationItem.title=#"First";
[localControllersArray addObject:navigation];
self.secondViewController = [[secondViewController alloc] initWithNibName:#"secondViewController" bundle:nil];
navigation = [[UINavigationController alloc] initWithRootViewController:secondViewController];
self.secondViewController.navigationItem.title=#"second";
[localControllersArray addObject:navigation];
self.ThirdViewController = [[Third ViewController alloc]initWithNibName:#"Third ViewController" bundle:nil];
navigation = [[UINavigationController alloc] initWithRootViewController:ThirdViewController];
self.secondViewController.navigationItem.title=#"Third";
[localControllersArray addObject:navigation];
tabbarcontroller.viewControllers = localControllersArray;
self.tabbarcontroller.delegate = self;
[self.tabbarcontroller setSelectedIndex:0];
[self.window addSubview:tabbarcontroller.view];
}

subview added to navigationbar ios

I have problems with displaying view as subview on navigationController.view on Ipad . I need to display view with transparent background on my viewController(with navBar),but while i change orientation my navBar become visible on foreGround of my view;
I created simple view based app.
Here code i added to project:
AppDelegate.h:
UINavigationController *_navController;
#property (strong, nonatomic) UINavigationController *navController;
AppDelegate.m:
_navController = [[[UINavigationController alloc] initWithRootViewController:self.viewController] autorelease];
self.window.rootViewController = _navController;
ViewController.m:
- (void)viewDidLoad
{
[super viewDidLoad];
UIView *view = [[[UIView alloc] initWithFrame:self.view.frame] autorelease];
[view setBackgroundColor:[UIColor redColor]];
[self.navigationController.view addSubview:view];
}
try pushing view to Navigation Controller
YourAppDelegate *del = (YourAppDelegate *)[UIApplication sharedApplication].delegate;
[del.navigationController pushViewController:nextViewController animated:YES];
or
UINavigationController* navigation = [[UINavigationController alloc] init];
iVkViewController *overviewViewController = [[iVkViewController alloc] init];
overviewViewController.title = #"First";
[navigation pushViewController:overviewViewController animated:NO];

Pushing View Controller with Two Nav Controllers

I've got an app where I am pushing a modal view controller. It is working fine, but I am concerned I haven't coded it in the most correct fashion. I have instanstiated two navigation controllers, which seems a bit dodgy to me.
Basically I've created a tab bar controller with 3 tabs, then made one of those tabs / view controllers the root. Later I am (using some home-grown markup on core text) popping a view controller when the user touches a particular word in a paragraph. The pushed view controller has a back button which works fine and the app seems to be OK.
Like I said it all works, but it seems I am coding in circles here. Is this correct?
AppDelegate.h
#import <Foundation/Foundation.h>
#interface AppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate>
{
UIWindow *window;
UITabBarController *tabBarController;
}
#property (strong, nonatomic) UIWindow *window;
#property (strong, nonatomic) UITabBarController *tabBarController;
#end
From AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
ViewController *viewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
ViewController2 *viewController2 = [[ViewController2 alloc] initWithNibName:#"ViewController2" bundle:nil];
ViewController3 *viewController3 = [[ViewController3 alloc] initWithNibName:#"ViewController3" bundle:nil];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:viewController];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:nav, viewController2, viewController3, nil];
self.tabBarController.delegate = self;
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
ViewController3.h
#import <UIKit/UIKit.h>
#import "JSCoreTextView.h"
#import "PopupViewController.h"
#class JSTwitterCoreTextView;
#interface ReadingViewController : UIViewController <JSCoreTextViewDelegate>
{
JSTwitterCoreTextView *_textView;
UIScrollView *_scrollView;
}
#end
From ViewController3.m
Here I am instantiating another navigation controller. Is this a good idea?
- (void)textView:(JSCoreTextView *)textView linkTapped:(AHMarkedHyperlink *)link
{
PopupViewController *popupVC = [[PopupViewController alloc] initWithNibName:#"PopupViewController" bundle:nil];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:popupVC];
[nav setModalPresentationStyle:UIModalPresentationFullScreen];
[nav setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
[self presentModalViewController:nav animated:YES];
}
From PopupViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
[self.navigationItem setRightBarButtonItem:[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:self
action:#selector(done:)]];
}
- (void)done:(id)sender
{
[self.parentViewController dismissModalViewControllerAnimated:YES];
}
It appears the answer is "yes". I was under the impression there is a single Navigation Controller for the app, but it's more like one per tab, depending on if there are going to be further pushes from that tab.

Create uiTabBarController programmatically

I want to create a UIView for a UITabBarController
Here is my code for the .h file :
#interface TE : UIViewController <UITabBarControllerDelegate>{
UITabBarController *tabBarController;
}
#property (nonatomic,retain) UITabBarController *tabBarController;
#end
The viewDidLoad method:
UIViewController *testVC = [[T1 alloc] init];
UIViewController *otherVC = [[T2 alloc] init];
NSMutableArray *topLevelControllers = [[NSMutableArray alloc] init];
[topLevelControllers addObject: testVC];
[topLevelControllers addObject: otherVC];
tabBarController = [[UITabBarController alloc] init];
tabBarController.delegate = self;
[tabBarController setViewControllers:topLevelControllers animated:NO];
tabBarController.selectedIndex = 0;
self.view = tabBarController.view;
This creates the tab bar controller, but when I click on a tab bar item, I get an error:
Thread1:Program receive signal: SIGABRT
Edit: I solved the problem by downloading and modifying the version of http://www.iphonedevcentral.com/create-uitabbarcontroller/
You say above that you don't want to create the tabBarController in the appDelegate. Why not? Where else would you create it? The tabBarController has to be the root view controller and cannot be a child of any other view controller.
Btw, make sure you implement:
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
NSUInteger tabIndex = [tabBarController.viewControllers indexOfObject:viewController];
if (viewController == [tabBarController.viewControllers objectAtIndex:tabIndex] ) {
return YES;
}
return NO;
}
Subclass UITabBarController
Override the - (void) loadView method and include the following code
MyCustomViewControllerOne* ctrl1 = [[[MyCustomViewControllerOne alloc] initWithNibName#"MyViewControllerOne" bundle: nil] autorelease];
UIViewController* ctrl2 = [[[UIViewController alloc] init] autorelease];
MyCustomControllerTwo* ctrl3 = [[[UIViewController alloc] initWithObject: myObj] autorelease];
ctrl1.title = #"First tab";
ctrl2.title = #"Second tab";
ctrl3.title = #"Third tab";
ctrl1.tabBarItem.image = [UIImage imageNamed:#"tab_image1.png"];
ctrl2.tabBarItem.image = [UIImage imageNamed:#"tab_image2.png"];
ctrl3.tabBarItem.image = [UIImage imageNamed:#"tab_image3.png"];
[self setViewControllers: #[ctrl1, ctrl2, ctrl3]];
That's pretty much it.
Change self.view = tabBarController.view; to
[self.view addSubview:tabBarController.view]; And it works correctly
Trying changing
self.view = tabBarController.view;
to
[self.view addSubview:tabBarController.view];
See if that helps.
Also try placing this in your -(void)loadView method
- (void)loadView {
UIView *mv = [[UIView alloc] initWithFrame:CGRectMake(0.0, 100.0, 320.0, 480.0)];
self.view = mv;
[mv release];
}
The reason you probably are experiencing a black screen is because you haven't initialized your UIView properly.
#Mehdi, just make your TE a UITabBarController instead of a UIViewController which then has a TabBarController in it. Makes it all the more easy to manage your TabBarController. To respond to some others who have indicated that you can have only one TabBarController as the window's rootViewController. That is not the case. A UITabBarController can be instantiated in multiple places where you need a second level menu navigation. Have a TabBar within a TabBar would not make sense, but having a left Navigation Menu and then having a TabBar on each menu item would make sense.

Resources