【问题标题】:Create a UIViewController from UITabbarItem and Storyboard从 UITabbarItem 和 Storyboard 创建一个 UIViewController
【发布时间】:2013-10-05 16:12:14
【问题描述】:

如果我按下第二个 UITabbarItem,我正在尝试打开 UIViewController。

这是我的第一个 ViewController 的代码。如果我按 UITabbarItem #1(标签 1),它会设置 Label.text。在 #2 我想切换到 secondViewController 并且(标签 2)它崩溃了。 有谁知道为什么?第一个断点位于 UIStoryboard...(故事板称为:Main.storyboard)。感谢您的帮助

#import "ViewController.h"
#import "secondViewController.h"
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.tabBar.delegate = self;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{
    switch (item.tag) {
        case 1:        self.testlabel.text =@"test";
            break;
        case 2:
        {  // create the Storyboard object
        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        secondViewController *second = [storyboard instantiateViewControllerWithIdentifier:@"secondview"];
        [self.navigationController pushViewController:second animated:YES];
        }         
        break;
        default:         break; }
}
@end

【问题讨论】:

  • 我想它现在可以工作了,但是有没有办法在没有导航控制器的情况下做到这一点?

标签: iphone ios objective-c xcode uiviewcontroller


【解决方案1】:

有几件事。

  1. 如果还没有为UITabBarController创建属性

  2. 除非您想离开UITabBarController,否则您不会推送视图控制器,否则您必须手动将视图控制器设置为 tabBar 项,或者在 tabBarController 中重新设置视图控制器数组。

    NSMutableArray* newControllers = [NSMutableArray arrayWithArray:self.tabBarController.viewControllers];

    [newControllers addObject:second];

    [self.tabBarController setViewControllers:newControllers Animation:NO];

  3. 您的UITabBarController 在故事板中?在您想要的选项卡上按住右键单击按钮并将其拖动到控制器上,然后将该控制器设置为选项卡的位置,如果这样做,您可以忽略上面的 1 和 2

  4. UITabBarController 并不意味着对每个选项卡执行不同的功能,因此让第一个选项卡设置标签而第二个选项卡显示 UIViewController 是一种不好的编码习惯,但这无关紧要。

在所有这些 UITabBarController 的东西之后,应用程序可能会崩溃,因为您的 UINavigationController 为零,或者您收到 “SecondView 不是值编码兼容错误” 或类似的东西,这个意味着在情节提要中将属性设置为不再存在的项目。因此,在情节提要中右键单击视图控制器并查看是否看到任何黄色警告标记,如果有,请点击它们旁边的“X”。

【讨论】:

  • 您好,感谢您的努力。我没有使用 UITabBarController。只是带有 2 x UITabbarItem 的 UITabBar。我试图在没有来自 Storyboard 的 UITabBarcontroller 的情况下执行此操作,因为我想稍后在每个 ViewController 中都有自定义 tabBars。使用 UITabbarcontroller 可以做到这一点吗?
  • 哦,我知道你在做什么,是的,这是可能的,如果你想为每个控制器设置自定义标签栏,那么你就在正确的轨道上,你有错误日志吗?这将有助于我了解您的设置 C:
  • [UIStoryboard storyboardWithName:@"Main" bundle:nil];是故事板的名称还是应该是 [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-10
  • 2013-12-26
  • 1970-01-01
  • 2018-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多