开发者

Navigation Controller + segment Controller : Unable to change the view

开发者 https://www.devze.com 2023-02-15 09:44 出处:网络
I have a root view controller which has few buttons. On click of these button, I shown up a tab bar controller with each controller showing the table view controller. I have achieved this. So basicall

I have a root view controller which has few buttons. On click of these button, I shown up a tab bar controller with each controller showing the table view controller. I have achieved this. So basically, for each table view controller I have a dedicated class. To go ahead, I replaced the tab bar controller, and added the segmented controller to the navigation title view.

The question is how can I set the view based on the selected index. I am able to set the navigation title to be segmented control but on select I am unable to set the view.

开发者_StackOverflow中文版

Below is what i have achieved so far. Note: What matters is a running code, I would do that code optimization later on. I dont want to hidde views. I want to call different view controller class.

RootViewController class (on click of the button, i call the first view controller. So that I can set the segment controller:

-(IBAction) pushSegmentController: (id) sender 
{

    NSLog(@"My Location Button being clicked/touched") ;

    FirstViewController *firstViewController = [[FirstViewController alloc] init] ;
    [self.navigationController pushViewController:firstViewController animated:YES];

    // Releae the view controllers
    [firstViewController release];

}

IN FirstViewController class:

-(void)viewDidLoad {

    [super viewDidLoad];

    NSArray *viewControllers = [self segmentViewControllers];


    self.segmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"Table1", @"Table2"]];

    self.navigationItem.titleView = segmentedControl;

    [self.segmentedControl addTarget:self 
                              action:@selector(indexDidChangeForSegmentedControl:) 
                    forControlEvents:UIControlEventValueChanged];

    self.segmentedControl.selectedSegmentIndex =  0;
}

-(void)indexDidChangeForSegmentedControl:(UISegmentedControl *)aSegmentedControl {

    NSUInteger index = aSegmentedControl.selectedSegmentIndex;

    if(index ==0) {
    UIViewController *table1Controller = [[AustraliaViewController alloc] initWithStyle:UITableViewStylePlain];
         **???? HOW SHOULD I SET THE VIEW OVER HERE... AS ITS A PART OF THE NAVIGATION CONTROLLER**
    }
    else { }
}

Note: I have tried using this option:

[navigationController setViewControllers:theViewControllers animated:NO];

But this option doesnt give me the right result. How should I go ahead with the same as I want to call a view controller class and set its view based on the selected index.


You probably don't want to have one view controller with different views depending on the button index, especially since you already have view controllers for your different screens.

If you want the table view controller to be pushed onto your navigation controller, so it will have a back button that gets you back to FirstViewController, use

-(void)indexDidChangeForSegmentedControl:(UISegmentedControl *)aSegmentedControl {
    NSUInteger index = aSegmentedControl.selectedSegmentIndex;

    UIViewController *newViewController = nil;
    if(index ==0) {
        newViewController = [[AustraliaViewController alloc] initWithStyle:UITableViewStylePlain];
    } else {
        newViewController = [[YourOtherViewController alloc] initWithStyle:UITableViewStylePlain];
    }
    [self.navigationController pushViewController:newViewController animated:YES];
}

If you'd rather have it slide in from the bottom and you want to handle setting up all necessary user interface (e.g. a dismiss button), replace that last line with

    [self presentModalViewController:newViewController animated:YES];


What about?

self.view = table1Controller;

or

[self.view addSubview:table1Controller];

I just saw one more mistake you have done. You are allocating a UIViewController, but initializing it like a tableViewController(with initWithStyle).

If it's a subclass of UITableViewController, alloc it with that, not UIViewController.

0

精彩评论

暂无评论...
验证码 换一张
取 消