【发布时间】:2011-07-08 21:07:51
【问题描述】:
您好,我正在学习如何在 Objective-C 中编写代码。我想将视图中文本字段的值存储在一个数组中:
NSArray* answers = [NSArray arrayWithObjects:fluidIntake.text, sleepQuality.text, sleepQuantity.text, mentalRecovery.text,
physicalRecovery.text, pretrainingEnergy.text, muscleSoreness.text, generalFatigue.text, nil];
这可能吗?如果没有,是否有任何更简洁的方法来存储多个文本字段值而无需将它们分配给 NSString 变量..
更新:
这是我的视图控制器中的功能:
-(IBAction)postData:(id)sender{
diary = [[PersonalDiary alloc]init];
NSArray* answers = [NSArray arrayWithObjects:fluidIntake.text, sleepQuality.text, sleepQuantity.text, mentalRecovery.text,
physicalRecovery.text, pretrainingEnergy.text, muscleSoreness.text, generalFatigue.text, nil];
[diary post:answers to: @"http://www.abcdefg.com/test.php"];
}
它在按下按钮时触发。它应该将输入到文本字段中的 8 个值存储在 NSArray 中,并将其传递给我的 Model 类中的一个函数,该函数会尝试打印数组中的第一个元素(即第一个文本字段值):
-(void) post:(NSArray*) ans to:(NSString*) link{
NSLog(@"%@", ans[0]);
}
但它没有,它只是打印:PersonalDiary[37114:207] __NSArrayI
【问题讨论】:
-
您同时发布了更多代码;我已经删除了我的答案,因为它不再有用了。 @Joe 和@Grady Player 的回答非常好。
标签: iphone objective-c xcode nsarray