子类化 UINavigationItem

发布于 2024-11-28 01:40:46 字数 966 浏览 3 评论 0原文

我有一个 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 可以工作,但其他似乎都不起作用。

I have a subclass for navigationItem because I want to change the text, font, color, etc. of the navigation title label.

I have the following for my customNavigationItem.h:

@interface CustomNavigationItem : UINavigationItem {

}
@end

and in the customNavigationItem.m I have just one method:

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

THE PROBLEM:
In the UIViewController, I initialized the navigation item in initWithNibName, connected it in the nib file, and put "CustomNavigationItem" as the custom class in IB.

The nslog works, but nothing else seems to be working.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

忆离笙 2024-12-05 01:40:46

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

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

在这种情况下,[super initWithTitle:]——类似这样:

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

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

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

You need to propagate the init method to the parent class. When you name your method initWithSomething, it should be calling either:

  • the designated initializer of your class on self, or
  • in this case, this is the designated initializer, and so should be calling the designated initializer of the parent class, on super.

In this case [super initWithTitle:] -- something like this:

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

Otherwise, the init code of the parent class is never executed, the class won't be initialized properly, and nothing is guaranteed to work.

After you've done that, check that your text color isn't nil, or the same as background, or 0% alpha if you're still having problems.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文