我可以将 pushViewController 与 UISegmentedControl 一起使用吗? [英] Can I use pushViewController with UISegmentedControl?

查看:43
本文介绍了我可以将 pushViewController 与 UISegmentedControl 一起使用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

-(IBAction) segmentedControlIndexChanged:(id)sender{if ([发件人选择的段索引] == 0){BDshelveController *shelve_page = [[BDshelveController alloc] initWithNibName: @"BDshelveController" bundle:nil];}else if([发件人selectedSegmentIndex] == 2){BDlibraryController *lib_page = [[BDlibraryController alloc] initWithNibName:@"BDlibraryController" bundle:nil];[self.navigationController pushViewController:lib_page 动画:YES];}}

解决方案

是的,您可以检查这些内容

  • 您在代码中遗漏了 segmentedIndex=1.

  • 在选定的段= 0

  • 时,您错过了呼叫PushViewController
  • 或者更有可能 self.NavigatonController 为零

  • 您的分段控件尚未将值更改与您的操作相关联

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

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:yourRootViewController];//<-- 取决于你是否有故事板、xib 等...你应该已经初始化了[self.window addSubview:navController.view];[navController 发布];//<-- 取决于你是否有 ARC[self.window makeKeyAndVisible];返回是;}

否则我想你可以通过将 self 作为根控制器来初始化它.

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

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

}

解决方案

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

这篇关于我可以将 pushViewController 与 UISegmentedControl 一起使用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆