0

I need UIToolbar when my application launches. It will be on every screen through out the application. So for that i have add it in RootViewController.

#import "AppDelegate.h"
#import "MainViewController.h"

@implementation AppDelegate

@synthesize window = _window;
@synthesize mainViewController = _mainViewController;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]autorelease];
// Override point for customization after application launch.
self.mainViewController = [[[MainViewController alloc]init]autorelease];
self.window.rootViewController = self.mainViewController;
[self.window makeKeyAndVisible];
return YES;
}

Currently i have UIToolbar in MainViewController

 // create the UIToolbar at the bottom of the MainViewController
toolbar = [UIToolbar new];
toolbar.barStyle = UIBarStyleBlackOpaque;
// size up the toolbar and set its frame
toolbar.frame = CGRectMake(0, 425, 320, 40);
[self.view addSubview:toolbar];

In all my other UIViewControllers i m adding it like this

[self.view addSubview:toolbar]; 

which i don't want. I want UIToolbar to show automatically in all my other UIViewControllers after attaching it to rootviewcontroller. so that when i flip UIViewController to change to another UIViewController. Only UIViewController should flip not UIToolbar.Right now UIToolbar is also flipping with UIViewController. So that is the reason i want to attach it to the rootviewcontroller.

So how can i add UIToolbar in APPDelegate file and attach it to rootviewcontroller.

Thanks for help.

user1120133
  • 3,244
  • 3
  • 48
  • 90

3 Answers3

2

Everything looks okay in your code except when you create the UIToolbar, for some reason at that point you make your own syntax. This is how you create a UIToolBar

Example:

UIToolbar *toolBar = [UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44);

Seriously, your code would not compile, and you would know at exactly what line. Connect the dots and Google how to create a UIToolbar.

Edit:

For the Toolbar being display in every view you have a few options:

(1) Are you sure you don't want a UINavigationController & UINavigationBar?

(2) Attach it at the UIWindow (i.e. add two subviews to the window). This has to be created with code, I do not believe interface builder is going to work.

(3) Just create it when each view loads in either (init or viewDidLoad methods)

Okay, so you would have to add the Toolbar to each view controller (i.e. addSubView). The only other way is to add two subViews to the AppDelegate.

Vikings
  • 2,527
  • 32
  • 45
  • If i do like UIToolbar *toolBar = [UIToolbar alloc] initWithFrame:CGRectMake(0, 425, 320, 40); As mainviewcontroller is attached to rootviewcontroller. self.window.rootViewController = self.mainViewController; now i can do like [self.mainViewController.view addSubview:toolbar]; to attach it to the rootviewcontroller and mainviewcontroller. – user1120133 May 19 '12 at 22:00
  • @user1120133 No that would not work. It would appear in the mainViewController, but once you navigation to a different view controller it would not be there unless you added it to that view controller to (i.e. addSubView) – Vikings May 19 '12 at 22:02
  • @user1120133 I think Vikings has it dead on. You can continue to reference the toolbar by saying self.toolbar.color = [UIColor RedColor]; although I would not call it "toolbar" because that would get confusing very quickly – Coder404 May 19 '12 at 22:04
  • @user1120133 Please not that when you add the toolbar to the mainViewController it only belongs to that view controller, if you want to physically see it in another view controller you must create a new one and add it via addSubView or add two subViews in the AppDelegate Check the edit. – Vikings May 19 '12 at 22:06
  • @user1120133 yes if a property was created you could access the toolbar from another view, but he wants to see the toolbar across ALL his views, that code you posted would change the toolbar to red, but you wouldn't see that until you navigated back to mainViewController – Vikings May 19 '12 at 22:09
  • So as per vikings i should add two subview to appdelegate one for mainviewcontroller and another subview for toolbar. right – user1120133 May 19 '12 at 23:10
1

I do not believe you say

toolbar = [UIToolbar new];

Also, have you googled how to make a UIToolbar programmatically?

Coder404
  • 742
  • 2
  • 7
  • 21
1

Only UIViewController should flip not UIToolbar.Right now UIToolbar is also flipping with UIViewController. So that is the reason i want to attach it to the rootviewcontroller.

I can think of two ways to do this:

  1. Use the toolbar provided by the navigation controller. You shouldn't try to access that toolbar directly -- use UIViewController's -setToolbarItems:animated: method to set up the items you want for each view controller. If you give each view controller the same set of toolbar items, you'll get the appearance that you're after. I did notice a slight flicker in my quick experiment, but this is a very quick and easy way to get a standard toolbar for all your views.

  2. Create your own container controller. With the introduction of iOS 5 we got the ability to write our own container controllers, i.e. view controllers that can host other view controllers inside themselves, just like UINavigationController and UITabBarController do. So, you could easily write your own container and have it host your toolbar. It sounds like this is what you're already attempting to do, but you have to realize that you don't get that functionality automatically.

Caleb
  • 124,013
  • 19
  • 183
  • 272