Can I use pushViewController with UISegmentedContr

2019-09-15 16:19发布

问题:

I want to use pushViewController with UIsegmentedController to push to other view and xib. Can I do that in xcode 4 ??? If I can , Could you guide me please ?

Here is some code that I try.But won't work.

-(IBAction) segmentedControlIndexChanged:(id)sender
{

    if ([sender selectedSegmentIndex] == 0) 
    {
        BDshelveController *shelve_page = [[BDshelveController alloc] initWithNibName: @"BDshelveController" bundle:nil];

    }
    else if([sender selectedSegmentIndex] == 2)
    {

        BDlibraryController *lib_page = [[BDlibraryController alloc] initWithNibName:@"BDlibraryController" bundle:nil];
        [self.navigationController pushViewController:lib_page animated:YES];
    }

}

回答1:

yes you can, these are the things you could check

  • you missed segmentedIndex=1 in your code.

  • you missed to call pushViewController when selected segment=0

  • Or it is more likely that self.NavigatonController is nil

  • Your segmented control has not been connected valuechange to your action

If self.navigationController is nil, it depends on how you initialize your app, if your app is navigation bar driven, then this code in your AppDelegate should resolve the issue:

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

    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:yourRootViewController]; // <-- depends if you have storyboards, xib etc...you should this already initialized
    [self.window addSubview:navController.view];
    [navController release]; // <-- depends if you have ARC
    [self.window makeKeyAndVisible];
    return YES;
}

Otherwise I suppose you can initialize it where you are, by giving self as root controller.

UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:self];