简体   繁体   中英

Use UISegmentedControl to switch to a MKMapView and UITableView

I'm making an app and i have a view controller with a UISegmentedControl, and a want to switch between a MKMapView and a UITableView. In the MKMapView i want to display a map with the users current location, and in the TableView i want to list some data. Thats it.

Sounds simple but i'm don't know how to proceed, i tried to make my view controller a tableview controller and then add the MKMapview, also tried to just add both views and a simple view controller. Anyway, there is a right or better way to do that?

Thanks guys!

You can use target-action to have the segmented control hide one view and unhide the other when it's value is changed:

- (void)segmentChanged:(id)sender
{
    switch ([sender selectedSegmentIndex]) {
        case 0:
        {
            self.tableView.hidden = NO;
            self.mapView.hidden = YES;
            break;
        }   
        case 1:
        {
            self.tableView.hidden = YES;
            self.mapView.hidden = NO;
            break;
        } 
        default:
            break;
    }
}

add both as subview

then whenever you want to switch just do

[self.view bringSubviewToFront:YOURVIEW];

The clean way would be to switch the subview, as soon as the button is pressed.

[view1 removeFromSuperView];
[self.view addSubview: view2];

For better performance you could save both views as a member variable, so they don't get instanciated every time.

You could even add a Viewtransition, when doing it in that way. (Eg flipping or fading)

Also in iOS5 you could write your own ViewControllerContainer. But thats way too complicated for that task.

I would use 2 navigationControllers.

Declare your first navigationController as usual, then when user tap the segmentedControl, create your tableController with another navigationController, and display it as modalViewController.

UINavigationController* modalController = [[UINavigationController alloc] initWithRootViewController:tableViewController];
[modalController setToolbarHidden:NO];
[self.navigationController presentModalViewController:modalController animated:YES];
[modalController release];

Then, when user tap the tableViewController's segmented control, just dismiss the viewController.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM