Home Tutorial Iphone Examples iPhone Segments Changes ToolBar And ToolBar Button

 
 

Share on Google+Share on Google+
iPhone Segments Changes ToolBar And ToolBar Button
Posted on: October 9, 2009 at 12:00 AM
Advertisement
In this tutorial will learn how to use the Segmented Controller to change the ToolBar Style and also by another Segmented Controller will change the ToolBar Button Style, this is View Based Application.

iPhone Segments Changes ToolBar And ToolBar Button

In this tutorial will learn how to use the Segmented Controller to change the ToolBar Style and also by another Segmented Controller will change the ToolBar Button Style, this is View Based Application.

Final View will look like this:

My project name is SegmentChangeToolbar, application is based on View Based Application, will create variable for the Segmented Controller and the Toolbar after declaring variable will write the property for the variables. After that will write the action for the Segmented Controller so that when the segment of Segmented Controller is pressed the Toolbar style and the style of the Toolbar Button is changed, so to do this will write the action for the Segmented Controller in .h file and the method for the action is written in .m file.

Add this to ViewController.h file:

UISegmentedControl            *barStyle;

UIToolbar                       *mtoolbar;

UIBarButtonSystemItem   currentSystemItem;

UISegmentedControl        *buttonStyle;

@property (nonatomic, retain) IBOutlet UISegmentedControl *barStyle;

@property (nonatomic, retain) UIToolbar   *mtoolbar;

@property (nonatomic, retain) IBOutlet UISegmentedControl *buttonStyle;

- (IBAction)BarStyle:(id)sender;

- (IBAction)ButtonStyle:(id)sender;

After declaring the variable for each controller, we have to synthesize it in .m file so that the getter and the setter is created for the specified variable, Then will add Toolbar to the view before this will create Toolbar and then will add it to the View. In viewDidLoad method will create the toolbar at the bottom of the view controller and also the size of the Toolbar and frame, add this to view.

Now will create the Items for the Toolbar which we want to show on to the Toolbar. This is done in createToolbarItems and after creating the items add it to the Toolbar. Action for the Segmented Controller is written by using Switch condition and on each cases we are changing the style of the Toolbar (BarStyle) and in ButtonStyle we are changing the Button style which is present on Toolbar, this is also done by using Switch condition. We are also writing the action for the Toolbar Button and then release all the variable created in .h file.

Add this to ViewController.m file:

@synthesize barStyle, mtoolbar, buttonStyle;

-(void)viewDidLoad

{

    [super viewDidLoad];

    mtoolbar = [UIToolbar new];

    mtoolbar.barStyle = UIBarStyleDefault;

    [mtoolbar sizeToFit];

    CGFloat toolbarHeight = [mtoolbar frame].size.height;

    CGRect mainViewBounds = self.view.bounds;

    [mtoolbar setFrame:CGRectMake(CGRectGetMinX(mainViewBounds),

CGRectGetMinY(mainViewBounds) + CGRectGetHeight(mainViewBounds) - (toolbarHeight * 1.0) + 2.0,

 CGRectGetWidth(mainViewBounds),toolbarHeight)];

    [self.view addSubview:mtoolbar];

    currentSystemItem = UIBarButtonSystemItemDone;

    [self createToolbarItems];

}

- (void)createToolbarItems

{  

 UIBarButtonItemStyle style = [self.buttonStyle selectedSegmentIndex];

UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:currentSystemItem target:self action:@selector(action:)];

    item.style = style;

 UIBarButtonItem *new = [[UIBarButtonItem alloc] initWithTitle:@"Item" style:style  target:self action:@selector(action:)];

    NSArray *items = [NSArray arrayWithObjects: item, new,  nil];

    [self.mtoolbar setItems:items animated:NO];

    [item release];

    [new release];

}

- (IBAction)BarStyle:(id)sender

{

switch ([sender selectedSegmentIndex])

    {

      case 0:

  mtoolbar.barStyle = UIBarStyleDefault;

            break;

      case 1:

            mtoolbar.barStyle = UIBarStyleBlackOpaque;

            break;

      case 2:

            mtoolbar.barStyle = UIBarStyleBlackTranslucent;

            break;

    }

}

- (IBAction)ButtonStyle:(id)sender

{

    UIBarButtonItemStyle style = UIBarButtonItemStylePlain;

    switch ([sender selectedSegmentIndex])

    {

      case 0:

      {

            style = UIBarButtonItemStylePlain;

            break;

      }

      case 1:

      {    

            style = UIBarButtonItemStyleBordered;

            break;

      }

      case 2:

      {

            style = UIBarButtonItemStyleDone;

            break;

      }

    }

    NSArray *toolbarItems = mtoolbar.items;

    UIBarButtonItem *item;

    for (item in toolbarItems)

    {

      item.style = style;

    }

}

- (void)action:(id)sender

{

    NSLog(@" Stop It ");

}

- (void)dealloc {

    [super dealloc];

    [mtoolbar release];

    [barStyle release];

    [buttonStyle release];

}

Now that the controller which we added and also which we created to add on to the view, should be connected with the variable and this is done in Interface Builder. So to connect the variable select the view controller xib and then right click on file owner and then connect it with the controller which is on to the View, then save and close the window.

Make connections with Interface Builder:

Finally press Build And Go Button

Download the code

Advertisement

Related Tags for iPhone Segments Changes ToolBar And ToolBar Button:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: October 9, 2009

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com