Objective C : 身份检查器中的自定义 class 创建一个 class 的新对象?

Objective C : Custom class in identity inspector creates a new object of that class?

在情节提要中,当我添加新视图(例如 TableView)时,我可以在身份检查器的 "Custom class" 字段中 select class。

如果我理解这个 class 的规则,我希望这个 class "answer" 发送到我的 table 视图的消息(即这个 class是我的 table viewcontroller),当我 运行 我的项目时,它似乎做我想做的事。

我的问题是:要做到这一点,我希望我的 Xcode 自动实例化我的控制器的一个对象 class 并且 "link" 这个对象到我的情节提要中的 GUI。

但是,我预计如果我用

覆盖我的控制器 class 的 init 方法
-(id) init
{
  self=[super init];
  NSLog(@"object controller created automatically");
  return self;
}

创建控制器对象时,我在输出中有字符串。 相反,我没有输出。 为什么会这样,代码有什么问题?

如果我理解你的问题,你希望在 viewController 初始化时 打印 消息。

为什么你不把代码写在viewDidLoad?

赞:

在你的YourControllerClass.m

-(void)viewDidLoad {

[super viewDidLoad];

NSLog(@"Controller created");

}

现在将情节提要中控制器的 class 设置为 YourControllerClass,并且在创建控制器时应打印该消息。

干杯

P.s.: 如果您仍然需要帮助或有疑问,请发表评论。

故事板设置的 UIView 从未调用过 init。 相反,您应该使用 - (void)awakeFromNib,您的插座已准备好使用。

- (void)awakeFromNib {
    [super awakeFromNib];
    NSLog(@"object controller created automatically");
}

来自 awakeFromNib 文档:

Objects that conform to the NSCoding protocol (including all subclasses of UIView and UIViewController) are initialized using their initWithCoder: method. All objects that do not conform to the NSCoding protocol are initialized using their init method.