hitheredude
hitheredude

Reputation: 939

How to add title and Done button on toolbar in a modal view

I try to present a modal view (InfoViewController) from a navigation view controller(DateViewController).

I add a toolbar on top of InfoViewContoller's view. Now I want to add a title "Info" and a "Done" button on the toolbar.(The Done button will perform the infoDismissAction method)

Can anyone give me som tips? Thanks a lot!

Here's code of DateViewController.h

#import <UIKit/UIKit.h>
#import "InfoViewController.h"
@interface DateViewController : UIViewController 
{
    InfoViewController *infoViewController;
}
@property (nonatomic, retain) InfoViewController *infoViewController;
@end

DateViewController.m

- (IBAction)modalViewAction:(id)sender{    
    if (self.infoViewController == nil)
        self.infoViewController = [[[InfoViewController alloc] initWithNibName:
                                   NSStringFromClass([InfoViewController class]) bundle:nil] autorelease];
    [self presentModalViewController:self.infoViewController animated:YES];
}

- (void)dealloc{
    if (self.infoViewController != nil)
    {
        [infoViewController release];
    }
    [super dealloc];
}

- (void)viewDidLoad{
    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"Info"  style:UIBarButtonSystemItemPlay target:self  action:@selector(modalViewAction:)] autorelease];
    [modalBarButtonItem release];
    [super viewDidLoad];
}

Here's InfoViewController.m

- (IBAction)infoDismissAction:(id)sender{
    [self.parentViewController dismissModalViewControllerAnimated:YES];
}

- (void)viewDidLoad {
    UIToolbar *toolBar;
    toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
    toolBar.frame = CGRectMake(0, 0, 320, 50);
    toolBar.barStyle = UIBarStyleDefault;
    [toolBar sizeToFit];        
    [self.view addSubview:toolBar]; 
    [toolBar release];
    [backButton release];
    [super viewDidLoad];
}

Upvotes: 4

Views: 8153

Answers (2)

Joey
Joey

Reputation: 535

I'd recommend setting up another UINavigationController with your InfoViewController and present the navigation controller as your modal view.

To answer your question you'd want to fill in your UIToolbar like this:

- (void)viewDidLoad {
  [super viewDidLoad];
  UIToolbar *toolBar;
  toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
  toolBar.frame = CGRectMake(0, 0, 320, 50);
  toolBar.barStyle = UIBarStyleDefault;
  [toolBar sizeToFit];        
  [self.view addSubview:toolBar]; 
  [toolBar release];

  UIBarButtonItem* bbiInfo = [[UIBarButtonItem alloc] initWithTitle:@"Info" style:UIBarButtonItemStyleBordered target:self action:@selector(tappedInfoButton)];
  UIBarButtonItem* flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
  UIBarButtonItem* bbiDone = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(tappedDoneButton)];
  NSArray* items = [[NSArray alloc] initWithObjects:bbiInfo, flexibleSpace, bbiDone, nil];
  [toolBar setItems:items];

  [items release];
  [bbiInfo release];
  [flexibleSpace release];
  [bbiDone release];
}

Upvotes: 4

Shrey
Shrey

Reputation: 1977

Try this code.

- (void)viewDidLoad {
       UIToolbar *toolBar;
            toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
            toolBar.frame = CGRectMake(0, 0, 320, 50);
            toolBar.barStyle = UIBarStyleDefault;
            [toolBar sizeToFit];      
UIBarButtonItem *flexibleSpace = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil] autorelease];
UIBarButtonItem *infoButton = [[[UIBarButtonItem alloc] initWithTitle:@"INFO" style:UIBarButtonItemStyleBordered target:self action:@selector(InfoAction:)] autorelease];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:@"DONE" style:UIBarButtonItemStyleBordered target:self action:@selector(doneAction:)];


            NSArray *barButton  =   [[NSArray alloc] initWithObjects:flexibleSpace,infoButton,flexibleSpace,doneButton,nil];
            [toolBar setItems:barButton];

        [self.view addSubview:toolBar];
        [toolBar release];
        [barButton release];
        barButton = nil;
        [super viewDidLoad];

}

Upvotes: 4

Related Questions