iPhone Segments Changes ToolBar And ToolBar Button


 

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.

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];

0

    currentSystemItem = UIBarButtonSystemItemDone;

    [self createToolbarItems];

}

1

- (void)createToolbarItems

{  

 UIBarButtonItemStyle style = [self.buttonStyle selectedSegmentIndex];

2

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:)];

3

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

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

    [item release];

4

    [new release];

}

- (IBAction)BarStyle:(id)sender

5

{

switch ([sender selectedSegmentIndex])

    {

6

      case 0:

  mtoolbar.barStyle = UIBarStyleDefault;

            break;

7

      case 1:

            mtoolbar.barStyle = UIBarStyleBlackOpaque;

            break;

8

      case 2:

            mtoolbar.barStyle = UIBarStyleBlackTranslucent;

            break;

9

    }

}

- (IBAction)ButtonStyle:(id)sender

0

{

    UIBarButtonItemStyle style = UIBarButtonItemStylePlain;

    switch ([sender selectedSegmentIndex])

1

    {

      case 0:

      {

2

            style = UIBarButtonItemStylePlain;

            break;

      }

3

      case 1:

      {    

            style = UIBarButtonItemStyleBordered;

4

            break;

      }

      case 2:

5

      {

            style = UIBarButtonItemStyleDone;

            break;

6

      }

    }

    NSArray *toolbarItems = mtoolbar.items;

7

    UIBarButtonItem *item;

    for (item in toolbarItems)

    {

8

      item.style = style;

    }

}

9

- (void)action:(id)sender

{

    NSLog(@" Stop It ");

0

}

- (void)dealloc {

    [super dealloc];

1

    [mtoolbar release];

    [barStyle release];

    [buttonStyle release];

2

}

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:

3

Finally press Build And Go Button

Download the code

4

Ads