我可以将pushViewController与UISegmentedControl一起使用吗?

时间:2012-03-09 07:12:37

标签: objective-c xcode uisegmentedcontrol pushviewcontroller

我想使用带有UIsegmentedController的pushViewController来推送到其他视图和xib。 我可以在xcode 4中做到吗???如果可以的话,你能指导我吗?

这是我尝试的一些代码。但是不起作用。

-(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 个答案:

答案 0 :(得分:0)

是的,你可以,这些是你可以检查的东西

  • 您在代码中错过了segmentedIndex = 1。

  • 当您选择了segment = 0

  • 时,您错过了调用pushViewController
  • 或者self.NavigatonController更可能是nil

  • 您的分段控件尚未将值更改为您的操作

如果self.navigationController为nil,则取决于您初始化应用程序的方式,如果您的应用程序是导航栏驱动的,那么AppDelegate中的此代码应该可以解决问题:

- (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;
}

否则我认为你可以通过自己作为根控制器来初始化它。

UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:self];
相关问题