views:

1020

answers:

3

I want to be able to switch between a UITableView and a standard UIView in a Parent View. Basically I have a UITableView that shows on load displaying a log of information. I want to have a segment control in the header of the Window that when the user taps, transitions to a Graph View which is a standard UIView.

The UIViewController in question needs the ability to be pushed from a RootViewController stack.

I have tried a number of things, but I can't seem to get this to work. Does anyone have any code examples or suggestions on how this could be done?

A: 

It sounds like you should have a parent view that contains the segment control and space for a child view. When the user selects a segment you would add the relevant view (table or graph) as a child of the view and set its rectangle so it occupies the child area.

-----------
| Segment |
-----------
|         |
|         |
|  Child  |
|         |
-----------
Andrew Grant
A: 

You don't have to change UIViewController at all to display it in a Navigation Controller. Your segmented controller should call a method that executes the following code:

UIViewController *myViewController = [[UIViewController alloc] init];
[self.navigationController pushViewController:myViewController animated:YES];
[myViewController release];

I don't have a link to an example, but I can answer further questions if necessary.

-Drew

Drew C
A: 

It's actually not as difficult as it first seemed. Basically if you have a ParentViewController with a blank view and a NavController on top. Place the segment control in the center view of the NavController.

Then you just simply switch ViewController views when the segment control changes. The only thing you have to do is keep a pointer to the currentview and remove that before adding the new view to the parentVC. Code below:

- (void)showView
{
    if (_currentView != nil)
        [_currentView removeFromSuperview];

    if (segmentControl.selectedSegmentIndex == 0)
    {
        if (_graph == nil)
            _graph = [[DrawGraphViewController alloc] initWithNibName:@"DrawGraphViewController" bundle:[NSBundle mainBundle]]; 

        self.navigationItem.rightBarButtonItem = nil;
        _currentView = _graph.view;

        [contentView addSubview:_graph.view];
        [_graph reloadGraph];
    }
    else if (segmentControl.selectedSegmentIndex == 1)
    {   
        if (_log == nil)
            _log = [[LogTableViewController alloc] initWithNibName:@"LogTableView" bundle:[NSBundle mainBundle]];

        self.navigationItem.rightBarButtonItem = self.editButtonItem;
        _currentView = _log.view;
        [contentView addSubview:_log.view];
        [_log loadData];
    }   
    else {
        if (_export == nil)
            _export = [[ExportViewController alloc] initWithNibName:@"ExportView" bundle:[NSBundle mainBundle]];

        _currentView = _export.view;
        [contentView addSubview:_export.view];
    }
}
KiwiBastard