Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 选项卡项图像和文本未显示在UITableViewController上_Iphone_Objective C_Uitabbar - Fatal编程技术网

Iphone 选项卡项图像和文本未显示在UITableViewController上

Iphone 选项卡项图像和文本未显示在UITableViewController上,iphone,objective-c,uitabbar,Iphone,Objective C,Uitabbar,我刚刚使用xcode项目模板创建了一个新的iPhone标签应用程序,没有故事板。我删除了项目模板生成的FirstViewController和SecondViewController 后来,我创建了名为MyFirstViewController(UITableViewController的子类)和MySecondViewController(UIViewController的子类)的新控制器,并将它们放在UINavigationController下 我对代码做了一点修改,现在看起来如下所示:

我刚刚使用xcode项目模板创建了一个新的iPhone标签应用程序,没有故事板。我删除了项目模板生成的FirstViewController和SecondViewController

后来,我创建了名为MyFirstViewController(UITableViewController的子类)和MySecondViewController(UIViewController的子类)的新控制器,并将它们放在UINavigationController下

我对代码做了一点修改,现在看起来如下所示:

appdelegate.m:

#import "MyFirstViewController.h"
#import "MySecondViewController.h"

....

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    //create view controllers
    UITableViewController * vc1 = [[MyFirstViewController alloc] initWithNibName:nil bundle:nil];
    UIViewController * vc2 = [[MySecondViewController alloc] initWithNibName:nil bundle:nil];

    //create navigation controllers
    UINavigationController *nav1 = [[UINavigationController alloc] initWithRootViewController:vc1];
    UINavigationController *nav2 = [[UINavigationController alloc] initWithRootViewController:vc2];

    //add nav controllers to tab bar controller
    self.tabBarController = [[UITabBarController alloc] init];
    self.tabBarController.viewControllers = @[nav1, nav2];
    self.window.rootViewController = self.tabBarController;
    [self.window makeKeyAndVisible];

    return YES;
}
tabBarItem在第一个选项卡上未显示

MyFirstViewController.m:

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Setting tabbar item and image here is not showing. It just show empty tab, no title and no image. why o why?
        self.tabBarItem.image = [UIImage imageNamed:@"someImage"];
        self.tabBarItem.title = @"someName";        
    }
    return self;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // This is showing just fine
        self.tabBarItem.image = [UIImage imageNamed:@"someImage"];
        self.tabBarItem.title = @"someName";
    }
    return self;
}
但是第二个标签工作得很好

MySecondViewController.m:

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Setting tabbar item and image here is not showing. It just show empty tab, no title and no image. why o why?
        self.tabBarItem.image = [UIImage imageNamed:@"someImage"];
        self.tabBarItem.title = @"someName";        
    }
    return self;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // This is showing just fine
        self.tabBarItem.image = [UIImage imageNamed:@"someImage"];
        self.tabBarItem.title = @"someName";
    }
    return self;
}

知道为什么UITableViewController的子类不能显示tabBarItem吗?同时,UIViewController可以很好地显示它。

在您的
didfishlaunchin
方法中

UITableViewController * vc1 = [[MyFirstViewController alloc] initWithNibName:nil bundle:nil];
但是在你的
MyFirstViewController

- (id)initWithStyle:(UITableViewStyle)style;

您需要在
initWithNibName
上调用此方法,因为此方法不存在,您的tabBarItem不会显示。

是。当然现在它正在工作。有时很难看到显而易见的东西。多谢维希