iphone - 子类化 UINavigationItem

标签 iphone objective-c subclass uinavigationitem

我有一个 navigationItem 的子类,因为我想更改导航标题标签的文本、字体、颜色等。

我的 customNavigationItem.h 有以下内容:

@interface CustomNavigationItem : UINavigationItem {

}
@end

customNavigationItem.m 中我只有一种方法:

-(id) initWithTitle:(NSString *)title{

NSLog(@"initWithTitle");

CGRect frame = CGRectMake(0, 0, 156, 44);

UILabel *label = [[[UILabel alloc] initWithFrame:frame] autorelease];
label.backgroundColor = [UIColor clearColor];
label.font = [DataSingleton sharedMySingleton].navigationTitleFont;
label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.0];
label.textColor = [DataSingleton sharedMySingleton].textColor;
label.text =  NSLocalizedString(@"Events", @"");  

self.titleView = label;

return self;
}

问题:UIViewController中,我在initWithNibName中初始化了导航项,将其连接到nib文件中,并将“CustomNavigationItem”作为IB中的自定义类。

nslog 可以工作,但其他似乎都不起作用。

最佳答案

您需要将 init 方法传播到父类。当您将方法命名为 initWithSomething 时,它应该调用:

  • self 上类的指定初始值设定项,或
  • 在本例中,这是指定的初始化器,因此应该在 super 上调用父类的指定初始化器。

在本例中[super initWithTitle:]——类似这样:

-(id)initWithTitle:(NSString *)title {
    if((self = [super initWithTitle:title])){
        //initializer code
    }
    return self;
}

否则,父类的初始化代码永远不会被执行,类将无法正确初始化,并且无法保证任何工作。

完成此操作后,请检查您的文本颜色是否为零,或是否与背景相同,如果仍有问题,则检查是否为 0% alpha。

关于iphone - 子类化 UINavigationItem,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6933628/

相关文章:

ios - 如何将单元格插入 UITableViewController

ios - 无法更改父类(super class)变量 "description"

java - 初始化子类字段

iphone - 使用 Storyboard时如何获取指向 appDelegate 内的 viewController 的指针

iphone - HitTest 以避免 subview 但希望另一个 subview 工作

iphone - cllocation 和 mkreversegeocoder

ios - 将工具栏定位在屏幕顶部,底部有正确的阴影

ios - ActivityIndi​​cator 边界图形错误

iphone - Cocos2dx 3.17 TileMap Basic Sample - 错误的tilecord位置,返回的对象层项目位置也错误

python - 在 numpy 中重新定义 *= 运算符