Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 为什么不是';t initWithCoder是否正确初始化项目?_Objective C_Ios_Xcode_Uistoryboard_Initwithcoder - Fatal编程技术网

Objective c 为什么不是';t initWithCoder是否正确初始化项目?

Objective c 为什么不是';t initWithCoder是否正确初始化项目?,objective-c,ios,xcode,uistoryboard,initwithcoder,Objective C,Ios,Xcode,Uistoryboard,Initwithcoder,我有一个子类UITableViewCell。我的子类包含几个UILabels。我希望我的子类将这些标签初始化为应用于我的每个表视图单元格的一些默认设置 我读到我应该为此使用initWithCoder。正在调用我的initWithCoder函数,我可以一步一步地遍历函数中的每一行,它看起来像是在运动。但是,当我运行应用程序时,我看不到任何应用的属性。最明显的是,字体没有改变。我只是认为我在UILabels上修改的任何属性实际上都没有被保存或显示 我将此子类与情节提要结合使用。我知道我的更改不会反映

我有一个子类
UITableViewCell
。我的子类包含几个
UILabel
s。我希望我的子类将这些标签初始化为应用于我的每个表视图单元格的一些默认设置

我读到我应该为此使用
initWithCoder
。正在调用我的
initWithCoder
函数,我可以一步一步地遍历函数中的每一行,它看起来像是在运动。但是,当我运行应用程序时,我看不到任何应用的属性。最明显的是,字体没有改变。我只是认为我在
UILabel
s上修改的任何属性实际上都没有被保存或显示

我将此子类与
情节提要
结合使用。我知道我的更改不会反映在故事板上,但在应用程序运行时也不会反映出来,尽管我的代码正在执行

编辑:我想提到的是,在尝试重写
initWithCoder
之前,我在这些子类中有一个实例方法,我在其中运行这个逻辑。我只需要在
cellforrowatinexpath
中调用该实例方法。这个方法是有效的,但我认为在这个子类中自动出现这个逻辑会很方便

有什么想法吗?我的代码如下:

#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
和标题:

#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
#导入
@接口ThreadListCell:UITableViewCell
@属性(非原子,弱)IBUILabel*文本;
@属性(非原子,弱)IBUILabel*标题;
@属性(非原子,弱)IBUILabel*位置;
@结束

当您收到消息时,没有一个IBoutlet被设置-您需要使用“awakeFromNib”或“viewDidLoad”之类的命令访问您的outlet。也就是说,您可以在initWithCoder中设置其他非出口内容。

哦,好的。将
initWithCoder
替换为
awakeFromNib
似乎可以正常工作。谢谢!尽管如此,它看起来像是在我在
cellforrowatinexpath
中设置任何属性之前发生的
awakeFromNib
。这让我觉得我的实例方法可能是更好的方法,因为我可以根据
UILabel
中的当前文本可靠地执行大小调整操作。顺序应该是:initWithCoder、awakeFromNib、cellforrowatinexpath。如果需要“预设”一些数学值,可以在init例程中进行。但在nib加载之前,您无法访问您的插座。