我想要做的是在我的iOS应用程序中自定义UITabBarItem的文本和图像。我想通过将UITabBarControllerCustomTabBar类一起子类化可以做到这一点。

当我尝试使用以下代码进行操作时,出现错误消息: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

最佳答案

您发布的代码在选项卡栏控制器子类中,因此选项卡栏控制器只是“自我”。

- (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";
}

09-08 09:06