【问题标题】:Add a UITabBarController to a UIViewController (Xcode 4.6)将 UITabBarController 添加到 UIViewController (Xcode 4.6)
【发布时间】:2013-02-01 03:10:34
【问题描述】:

我正在学习 Objective-C,并在学习的同时开始创建一个应用程序。 我创建了一个“单视图应用程序”,其中包含两个名为“ViewController”和“secondViewController”的“视图控制器”,之后我想添加一个“UITabBarController”,所以按照教程我在 AppDelegate.m 中使用了以下代码/p>

(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

[self customizeInterface];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

UITabBarController *tabController = [[UITabBarController alloc] init];

UIViewController *viewController1 = [[UIViewController alloc] init];
UITabBarItem *tab1 = [[UITabBarItem alloc] initWithTitle:@"Artists" image:[UIImage imageNamed:@"artist-tab.png"] tag:1];
[viewController1 setTabBarItem:tab1];

UIViewController *viewController2 = [[UIViewController alloc] init];
UITabBarItem *tab2 = [[UITabBarItem alloc] initWithTitle:@"Music" image:[UIImage imageNamed:@"music-tab.png"] tag:2];
[viewController2 setTabBarItem:tab2];

tabController.viewControllers = [NSArray arrayWithObjects:viewController1,
                                 viewController2, nil];


self.window.rootViewController = tabController;

[self.window makeKeyAndVisible];


// Override point for customization after application launch.
return YES;
}

这里是 ViewController.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

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

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

@end

如果我运行应用程序,标签栏正在工作,但我有一个空白屏幕,而不是我创建的 ViewController。 就因为我是菜鸟,能否请您告诉我如何在每个选项卡中分别链接或显示视图控制器?

请把所有显而易见的事情都说出来。 提前谢谢你

【问题讨论】:

    标签: objective-c xcode uiviewcontroller uitabbarcontroller


    【解决方案1】:

    由于您是初学者,我强烈建议您使用情节提要,这将有助于更轻松地管理界面,因为您几乎不需要编写任何代码。

    http://www.raywenderlich.com/5138/beginning-storyboards-in-ios-5-part-1

    我只是猜测,但您的问题可能是您直接 init 您的视图控制器,您应该在您的 didFinishLaunchingWithOptions 中调用 initWithNibName: 方法。 initWithNibName: 是您从 nib 文件创建控制器实例时调用的名称。

    所以基本上 initWithNibName: 在加载和实例化 NIB 时被调用。

    我不知道您的 ViewContoller 类名,但您应该更改视图控制器的 init 方法

    在您的应用 delegate.m 中

    #import "ViewController.h"
    #import "secondViewController.h" 
    
    
        UIViewController *viewController1 = [[ViewController alloc] initWithNibName:@"ViewController" bundle:[NSBundle mainBundle]]; // make sure on interface builder hat you Nib name is ViewController
    
        UIViewController *viewController2 = [[secondViewController alloc] initWithNibName:@"secondViewController" bundle:[NSBundle mainBundle]];// make sure on interface builder hat you Nib name is secondViewController
    

    请使用大写字符和类名的详细名称 secondViewController 是不好的编程习惯

    【讨论】:

    • 非常感谢您的详细解答。我忘了告诉你我已经使用了故事板,但我认为使用以前的代码可能会更容易在以后添加任何进一步的自定义,但也许我错了。如果我将代码中的行替换为以下两行,则在开始时(加载屏幕之后)应用程序崩溃。这是我写的:UIViewController *viewController1 = [[UIViewController alloc] initWithNibName:@"ViewController" bundle:nil]; UIViewController *viewController1 = [[UIViewController alloc] initWithNibName:@"secondViewController" bundle:nil];
    • 这是因为在自定义类中,我为第一个视图控制器窗口放置了“ViewController”,为第二个视图控制器窗口放置了“secondViewController”。
    • 如果您使用情节提要,您需要将 bundle=nil 更改为 [NSBundle mainBundle] 我已编辑答案再看一遍。并单击情节提要并单击您的视图,并确保其情节提要 ID 与您的班级名称匹配
    • 这就是我现在所拥有的,使用更新后的代码:“***由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'无法在捆绑包中加载NIB:'NSBundle / meme/Library/Application Support/iPhone Simulator/6.0/Applications/08CBEFA8-8E7F-442F-BFAC-1D1360301076/MyFirstApp.app>(已加载)'名称为'ViewController'“。
    • 它说它找不到一个名为 'ViewController' 的笔尖,我很抱歉,但是你不明白这其中的哪一部分 click storyboard and click your views and make sure their Storyboard ID's matches with your class name 你必须设置你的视图的 ID,自定义类和你的一样类名。
    【解决方案2】:

    标签栏控制器显示您在代码中创建的视图。这是预期的行为,因为您正在创建全新的视图。

    如果您在 Interface Builder 中创建了“ViewController”和“secondViewController”,那么您必须加载 nib——而不是创建新视图——并告诉标签栏控制器使用它们。

    基本上你必须改变你的台词:

    UIViewController *viewController1 = [[UIViewController alloc] init];

    类似

    UIViewController *viewController1 = [[UIViewController alloc] initWithNibName:@"view1" bundle:nil];

    详情请见initWithNibName:bundle:。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-01
      • 2013-08-11
      • 1970-01-01
      • 1970-01-01
      • 2020-12-05
      • 1970-01-01
      • 2011-10-02
      • 1970-01-01
      相关资源
      最近更新 更多