How to change centerview on click of tableview row

2019-06-10 06:02发布

问题:

am using MFSideMenu for my iphone app to implement a side bar. I have successfully implemented the sidebar. with the following code in my appdelegete

- (ViewController *)demoController {
    return [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
}

- (UINavigationController *)navigationController {
    return [[UINavigationController alloc]
            initWithRootViewController:[self demoController]];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

      self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

        LeftSlideViewController *leftMenuViewController = [[LeftSlideViewController alloc] init];


        MFSideMenuContainerViewController *container = [MFSideMenuContainerViewController
                                                        containerWithCenterViewController:[self navigationController]
                                                        leftMenuViewController:leftMenuViewController
                                                        rightMenuViewController:nil];


        container.navigationController.navigationItem.rightBarButtonItem=nil;
        self.window.rootViewController = container;
        [self.window makeKeyAndVisible];

        return YES;
}

Now Let me explain my problem..

I have View-controller name with LeftSideViewController in which there is UITablview And on application starts there is one view-controller as a center view named as "ViewController" I don't need right-side view controller so i dint include that

now I have UITableview in my leftsidecontroller and on click of that UITableview's row i have to change the center view.

My problem is that when i put this code in my leftside view controller's uitableview delegete method as this its not changing my centerview with new view controller

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
      // Load new front view
          MFSideMenuContainerViewController *menuController = (MFSideMenuContainerViewController*)((AppDelegate*)[[UIApplication sharedApplication] delegate]).viewController;

        PhotoGalleryViewController *SearchBarTableView=[[PhotoGalleryViewController alloc]initWithNibName:@"PhotoGalleryViewController" bundle:nil];
        [self.navigationController pushViewController:SearchBarTableView animated:YES];
        UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:SearchBarTableView];
        menuController.centerViewController=navController;
        //[menuController1 setRootController:navController animated:YES];
        [tableView deselectRowAtIndexPath:indexPath animated:YES];

}

Can anyone please give me some guidance that how to change the views accordingly the leftside view's UITableview row click.

回答1:

I am using MFSideMenu too. Here's how I'm handling the didSelectRowAtIndexPath...

Although, I'm using storyboard in mine. But I also have viewcontrollers embedded within NavigationControllers like you do, and I found this method of transitioning to work.

if (indexPath.section ==1 ){
    if (indexPath.row==0){
        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:[NSBundle mainBundle]];
        UITabBarController *destNav = [storyboard instantiateViewControllerWithIdentifier:@"MainTabBar"];
        [self.menuContainerViewController setCenterViewController:destNav];
    }
    if (indexPath.row==1){
        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:[NSBundle mainBundle]];
        UINavigationController *destNav = [storyboard instantiateViewControllerWithIdentifier:@"TopNavID"];
        [self.menuContainerViewController setCenterViewController:destNav];
    }
    if (indexPath.row == 2){

        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:[NSBundle mainBundle]];
        UINavigationController *destNav = [storyboard instantiateViewControllerWithIdentifier:@"OptionsNavID"];
        [self.menuContainerViewController setCenterViewController:destNav];

    }
}


[self.menuContainerViewController setMenuState:MFSideMenuStateClosed];


回答2:

you should implement delegate method from your centerview and LeftSideViewController. so the one that actually change view is method from your centerview. the code inside my didSelectRowAtIndexPath:

else if (indexPath.section==1 && indexPath.row==0) {
    [delegate chooseCategory:2];

}
else if (indexPath.section==1 && indexPath.row==1) {
    [delegate chooseCategory:3];

}
else if (indexPath.section==1 && indexPath.row==2) {
    [delegate chooseCategory:15];

}

chooseCategory is a method from centerview to change it's views.