【问题标题】:Using nibs and storyboards together一起使用笔尖和情节提要
【发布时间】:2013-12-25 21:00:52
【问题描述】:

根据我在互联网上阅读的内容,笔尖和故事板在同一个项目中可以相互比较。现在我正在创建一个选项卡式应用程序,并希望执行以下操作:

Tab 1:用笔尖构造,

标签 2:用故事板构建,

标签 3:用笔尖构造

最初我使用 nibs 创建了所有内容,因此在我的委托中,我使用以下代码从一个选项卡传递到下一个选项卡:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
    UIViewController *viewController1, *viewController2, *viewController3;
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        viewController1 = [[[FirstViewController alloc] initWithNibName:@"FirstViewController_iPhone" bundle:nil] autorelease];
        viewController2 = [[[SecondViewController alloc] initWithNibName:@"SecondViewController_iPhone" bundle:nil] autorelease];
        viewController3 = [[[ThirdViewController alloc] initWithNibName:@"ThirdViewController_iPhone" bundle:nil] autorelease];
    } else {
        viewController1 = [[[FirstViewController alloc] initWithNibName:@"FirstViewController_iPad" bundle:nil] autorelease];
        viewController2 = [[[SecondViewController alloc] initWithNibName:@"SecondViewController_iPad" bundle:nil] autorelease];
        viewController3 = [[[ThirdViewController alloc] initWithNibName:@"ThirdViewController_iPad" bundle:nil] autorelease];

    }
    self.tabBarController = [[[UITabBarController alloc] init] autorelease];
    self.tabBarController.viewControllers = @[viewController1, viewController2, viewController3];
    self.window.rootViewController = self.tabBarController;
    [self.window makeKeyAndVisible];
    return YES;



}

我需要 SecondViewController_.storyboard,而不是 SecondViewController_.xib。我该怎么做呢?我可以将名称“SecondViewController_.nib”更改为“SecondViewController_.storyboard”吗?我不认为这会起作用..

任何帮助将不胜感激!

编辑: 我使用的代码:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = NSLocalizedString(@"Events", @"Events");
        self.tabBarItem.image = [UIImage imageNamed:@"second"];
    }
    return self; }

【问题讨论】:

    标签: ios storyboard uitabbarcontroller xib nib


    【解决方案1】:

    您可以按照您的建议创建一个故事板,然后在其中创建一个SecondViewController 场景。在代码中,您可以将 initWithNibName: 替换为对 storyboardWithName:instantiateViewControllerWithIdentifier: 的调用。

    但是,由于故事板的一个主要功能是能够将多个控制器联系在一起并定义它们之间的关系,所以很难想象为什么为一个控制器做这件事会是一个好主意。

    【讨论】:

    • 感谢菲利普的评论!恐怕我不明白我需要做什么。首先,“在里面创建一个 SecondViewController 场景”是什么意思?我必须用“instantiateViewControllerWithIndentifier:”替换什么?
    • 1) 当您将视图控制器放入情节提要时,它会创建一个与类型或控制器的子类匹配的“场景”。因此,“SecondViewController 场景”是您将视图控制器设为SecondViewController 的场景之一。 2)您当前调用initWithNibName:@"SecondViewController_iPhone"...我是说您需要将其删除并改为调用其他两种方法。
    • 好的,第 1 部分很清楚。现在唯一的问题是第 2 部分。我在哪里放置 storyboardWithName: 以及我在哪里放置 instantiateViewControllerWithIdentifier: ?
    • @DCB 您应该接受 Phillip 的回答(单击旁边的复选标记),甚至可以投票。见What should I do when someone answers my question?
    【解决方案2】:

    正如菲利普所说,只需使用instantiateViewControllerWithIdentifier

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
        // Override point for customization after application launch.
        UIViewController *viewController1, *viewController2, *viewController3;
        UIStoryboard *storyboard;
        if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
            viewController1 = [[[FirstViewController alloc] initWithNibName:@"FirstViewController_iPhone" bundle:nil] autorelease];
            storyboard      = [UIStoryboard storyboardWithName:@"iPhone" bundle:nil];
            viewController2 = [storyboard instantiateViewControllerWithIdentifier:@"Second"];
            viewController3 = [[[ThirdViewController alloc] initWithNibName:@"ThirdViewController_iPhone" bundle:nil] autorelease];
        } else {
            viewController1 = [[[FirstViewController alloc] initWithNibName:@"FirstViewController_iPad" bundle:nil] autorelease];
            storyboard      = [UIStoryboard storyboardWithName:@"iPad" bundle:nil];
            viewController2 = [storyboard instantiateViewControllerWithIdentifier:@"Second"];
            viewController3 = [[[ThirdViewController alloc] initWithNibName:@"ThirdViewController_iPad" bundle:nil] autorelease];
    
        }
        self.tabBarController = [[[UITabBarController alloc] init] autorelease];
        self.tabBarController.viewControllers = @[viewController1, viewController2, viewController3];
        self.window.rootViewController = self.tabBarController;
        [self.window makeKeyAndVisible];
        return YES;
    }
    

    这假设您的故事板被称为iPhone.storyboardiPad.storyboard,但是更改上面的代码以匹配您对这两个故事板的称呼。此外,这再次假设您将场景定义为具有Second 的“故事板 ID”(您可以通过在 IB 中选择视图控制器并转到“身份检查器”来实现),并且您还指定了“自定义类” "也有:


    对于SecondViewController,您应该将initWithNibName:bundle: 方法替换为:

    - (id)initWithCoder:(NSCoder *)aDecoder
    {
        self = [super initWithCoder:aDecoder];
    
        if (self) {
            self.title = NSLocalizedString(@"Events", @"Events");
            self.tabBarItem.image = [UIImage imageNamed:@"second"];
        }
    
        return self; 
    }
    

    【讨论】:

    • @DCB 如果您原谅这篇社论,我必须说,虽然上面的技术向您展示了如何结合故事板和 NIB,但我建议您继续过渡到故事板。如果您一开始就将所有内容都转移到情节提要中,您可能会浪费更多时间试图弄清楚如何使两者共存。
    • 太棒了!谢谢!现在我遇到了一个问题。在底部的标签栏上,我不再有标签的图标或名称了。之前我用某个代码调用它。这不起作用,因为它使用的是笔尖。我该如何解决? (我已经用我正在使用的代码更新了问题)
    • @DCB 对于基于故事板的视图控制器(第二个),使用initWithCoder 而不是initWithNibName
    • 但是在我的 .h 中我现在有 - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { // 初始化代码 } return self; }
    • @DCB 您是否确认场景的视图控制器基类已设置为SecondViewController(参见上面的身份检查器屏幕快照)?也许在initWithCoder(当你将控制器添加到标签栏控制器时会被调用)和viewDidLoad(当你在第二个标签上标签时会被调用)中放置断点并确认视图控制器被正确实例化.但是我确认这种技术可以正常工作,所以你一定是在你的项目中忽略了一些简单的东西。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-02-14
    • 1970-01-01
    • 1970-01-01
    • 2012-01-24
    • 2020-04-18
    • 1970-01-01
    相关资源
    最近更新 更多