在'CustomTabBar'类型的对象上找不到属性'window'

时间:2014-08-28 19:29:54

标签: ios objective-c uitabbarcontroller

我想要做的是在我的iOS应用中自定义UITabBarItem的文字和图片。我想通过将UITabBarController子类化为CustomTabBar类来实现这一点。

当我尝试使用以下代码时,我收到一条错误消息:Property 'window' not found on object of type 'CustomTabBar'。如何才能正确地将UITabBarController子类化,以便我可以自定义文本和图像?

CustomTabBar.h:

#import <UIKit/UIKit.h>
#import <Parse/Parse.h>
#import <Parse/PFCloud.h>

@interface CustomTabBar : UITabBarController

@end

CustomTabBar.m:

#import "CustomTabBar.h"

@interface CustomTabBar ()

@end

@implementation CustomTabBar

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    // Assign tab bar item with titles
    UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
    UITabBar *tabBar = tabBarController.tabBar;
    UITabBarItem *tabBarItem1 = [tabBar.items objectAtIndex:0];
    UITabBarItem *tabBarItem2 = [tabBar.items objectAtIndex:1];

    tabBarItem1.title = @"Search";
    tabBarItem2.title = @"MatchCenter";
}

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

@end

1 个答案:

答案 0 :(得分:2)

您发布的代码位于标签栏控制器子类中,因此标签栏控制器只是&#34; self&#34;。

- (void)viewDidLoad {
    [super viewDidLoad];

    UITabBar *tabBar = self.tabBar;
    UITabBarItem *tabBarItem1 = [tabBar.items objectAtIndex:0];
    UITabBarItem *tabBarItem2 = [tabBar.items objectAtIndex:1];

    tabBarItem1.title = @"Search";
    tabBarItem2.title = @"MatchCenter";
}