Can I use pushViewController with UISegmentedControl?

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.

  • App Store code runs different from Xcode/Device code on iPhone 3G
  • Convert string to float in Objective-C
  • Why an empty implementation of drawRect: will adversely affect performance during animation
  • How to implement Single Sign-On on iOS
  • UITextView content size different in iOS7
  • How does NSDictionary handle NIL objects?
  • -(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];

    Solutions Collect From Internet About “Can I use pushViewController with UISegmentedControl?”

    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 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];