初始化时将参数传递给自定义类 [英] Passing parameters to a custom class on initialization
问题描述
我有一个 现在,我这样做: 如何在创建实例时传递值? 请帮助。 当然这假定你的参数是NSString类型,设置正确;) I have a class Right now, I am doing this: How can I pass the values at the creation of an instance?
I tried to redefine Please help. You have to create a custom initializer. of course this assumes your parameters are of NSString type and that you have your properties set correctly ;) 这篇关于初始化时将参数传递给自定义类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!消息
,有两个属性, name
和 message
和另一个类具有两个文本字段
nameField
和 messageField
/ code>。
我想在 MessageController
中创建一个 Message
的实例,传递这两个属性作为参数。 p>
Message * messageIns = [Message alloc] init];
messageIns.name = nameField;
messageIns.message = MessageField;
我试图在Message.m中重新定义 init
,但我不知道如何做。
- (id)init {
if((self = [super init])){
}
return self;
}
- (id)initWithName:必须创建一个自定义初始化程序。 (NSString *)name_ message:(NSString *)message_
{
self = [super init];
if(self){
self.name = name_;
self.message = message_;
}
return self;
}
Message
with two attributes, name
and message
, and another class MessageController
with two text fields, nameField
and messageField
.
I want to make an instance of Message
in MessageController
, passing these two attributes as arguments. Message *messageIns = [[Message alloc] init];
messageIns.name = nameField;
messageIns.message = MessageField;
init
in Message.m, but I don't know how do that.-(id)init{
if((self=[super init])){
}
return self;
}
-(id)initWithName:(NSString *)name_ message:(NSString *)message_
{
self = [super init];
if (self) {
self.name = name_;
self.message = message_;
}
return self;
}