xcode 为什么 initWithCoder 不能正确初始化项目?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11926862/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why isn't initWithCoder initializing items correctly?
提问by Ryan
I've got a subclass of UITableViewCell
. My subclass contains several UILabel
s. I want my subclass to initialize these labels to some default settings that applies to every table view cell of mine.
我有一个UITableViewCell
. 我的子类包含几个UILabel
s。我希望我的子类将这些标签初始化为一些适用于我的每个表格视图单元格的默认设置。
I read that I should be using initWithCoder
for this. My initWithCoder
function is being called, and I can step through each line in my function and it appears to go through the motions. When I run my application I do not see any of the properties being applied, though. Most noticeably, the font is not being changed. I just don't think any of the properties that I'm modifying on my UILabel
s are actually being saved, or displayed.
我读到我应该initWithCoder
为此使用。我的initWithCoder
函数正在被调用,我可以逐行浏览我的函数中的每一行,它似乎通过了动作。但是,当我运行我的应用程序时,我没有看到任何正在应用的属性。最值得注意的是,字体没有改变。我只是认为我在UILabel
s 上修改的任何属性实际上都没有被保存或显示。
I'm using this subclass in conjunction with a Storyboard
. I know my changes will not be reflected on the Storyboard
, but they're also not being reflected when the application runs - despite my code being executed.
我将此子类与Storyboard
. 我知道我的更改不会反映在Storyboard
.
Edit: I wanted to mention that prior to trying to override initWithCoder
, I had an instance method in these subclasses that I'd run this logic in. I would just call that instance method inside of cellForRowAtIndexPath
. This method was working, but I thought it'd be handy to have this logic in this subclass occur automatically.
编辑:我想提一下,在尝试覆盖之前initWithCoder
,我在这些子类中有一个实例方法,我将在其中运行此逻辑。我只会在cellForRowAtIndexPath
. 这个方法是有效的,但我认为在这个子类中自动出现这个逻辑会很方便。
Any ideas? My code is below:
有任何想法吗?我的代码如下:
#import "ThreadListCell.h"
@implementation ThreadListCell
- (id)initWithCoder:(NSCoder *)aDecoder {
if ((self = [super initWithCoder:aDecoder])) {
// adjust the font settings of the title
self.title.font = [UIFont fontWithName:@"SourceSansPro-Black" size:16];
self.title.textColor = [UIColor colorWithWhite:0.267f alpha:1.0f];
// adjust the font settings of the subtitle
self.text.font = [UIFont fontWithName:@"SourceSansPro-Light" size:14];
self.text.textColor = [UIColor colorWithWhite:0.267f alpha:0.9f];
self.text.textColor = [UIColor colorWithRed:1 green:0 blue:0 alpha:1];
// adjust the font settings of the location
self.location.font = [UIFont fontWithName:@"SourceSansPro-Light" size:8];
self.location.textColor = [UIColor colorWithWhite:0.267f alpha:0.9f];
// adjust the UILabel settings of the title
self.title.numberOfLines = 0;
self.title.lineBreakMode = UILineBreakModeTailTruncation;
// adjust the UILabel settings of the subtitle
self.text.numberOfLines = 0;
self.text.lineBreakMode = UILineBreakModeTailTruncation;
// adjust the UILabel settings of the location
self.location.numberOfLines = 0;
self.location.lineBreakMode = UILineBreakModeTailTruncation;
self.location.textAlignment = UITextAlignmentRight;
}
return self;
}
@end
And the header:
和标题:
#import <UIKit/UIKit.h>
@interface ThreadListCell : UITableViewCell
@property (nonatomic, weak) IBOutlet UILabel *text;
@property (nonatomic, weak) IBOutlet UILabel *title;
@property (nonatomic, weak) IBOutlet UILabel *location;
@end
回答by David H
None of your IBOutlets is set when you get the message - you need to use "awakeFromNib" or "viewDidLoad" - something like that - to access your outlets. That said, you can set other non-outlet stuff in the initWithCoder.
当您收到消息时,您的任何 IBOutlets 都没有设置 - 您需要使用“awakeFromNib”或“viewDidLoad” - 类似的东西 - 来访问您的插座。也就是说,您可以在 initWithCoder 中设置其他非出口的东西。