Can I use pushViewController with UISegmentedControl?

荒凉一梦 提交于 2019-12-13 05:49:18

问题


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


来源:https://stackoverflow.com/questions/9630272/can-i-use-pushviewcontroller-with-uisegmentedcontrol

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!