LuckyLuke
LuckyLuke

Reputation: 49077

UIToolbar and UINavigationController

I need to display a view modally. The viewcontroller that needs to be displayed modally has to have a UIToolbar at the bottom. In this toolbar there are one uisegmentedcontroller with three elements. (Think of an tabbar).

In the viewcontroller that presents the modal viewcontroller I have:

-(IBAction)presentModally:(id)sender {  

if (self.nvc == nil) {
    MyModalViewController *vc = [[MyModalViewController alloc] init];
    UINavigationController *navvc = [[UINavigationController alloc] initWithRootViewController:vc];
    navvc.navigationItem.prompt = @"";
    navvc.navigationBar.barStyle = UIBarStyleBlack;
    [vc release];
    self.nvc = navvc;
    [navvc release];
}

[self presentModalViewController:self.nvc animated:YES];

}

MyModalViewController:

- (void)loadView {
[super loadView];

UIView *uiview = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 460.0f)];
uiview.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.view = uiview;
[uiview release];

UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0.0f, 436.0f, 320.0f, 44.0f)];
toolbar.barStyle = UIBarStyleBlack;
toolbar.autoresizingMask = UIViewAutoresizingFlexibleWidth;

NSArray *itemArray = [NSArray arrayWithObjects: @"One", @"Two", @"Three", nil];
UISegmentedControl *segCon = [[UISegmentedControl alloc] initWithItems:itemArray];
segCon.frame = CGRectMake(60, 4, 200, 36);
segCon.segmentedControlStyle = UISegmentedControlStyleBar;
segCon.tintColor = [UIColor darkGrayColor];
segCon.selectedSegmentIndex = 0;
[segCon addTarget:self action:@selector(changedSegment:) forControlEvents:UIControlEventValueChanged];

[toolbar addSubview:segCon];
self.segmentedControl = segCon;
[segCon release];

[[self navigationController].view addSubview:toolbar];
[toolbar release];
}


- (void)changedSegment:(id)sender {

UISegmentedControl *control = (UISegmentedControl *)sender;
int index = control.selectedSegmentIndex;

[[self navigationController] popViewControllerAnimated:NO];
[[self navigationController] pushViewController:[self.controllers objectAtIndex:index] animated:NO];
}

The viewcontrollers in the array are just normal UIViewControllers.

I have set this property in those classes to:

self.navigationItem.hidesBackButton = YES;

My question: Is this the proper way to achieve a UITabBarController behavior?

Upvotes: 1

Views: 909

Answers (1)

Felix
Felix

Reputation: 35384

Haven't tested it, but It looks good, except that popViewControllerAnimated. I'd use popToRootViewControllerAnimated instead (In case a controller uses itself the navigationController).

Upvotes: 2

Related Questions