编译器没有给我任何错误,代码运行。好奇的是,如何在添加新元素后检查数组的内容。
在我的.h文件中
@interface AddCardViewController : UIViewController <UITextFieldDelegate>
@property (strong, nonatomic) IBOutlet UITextField *cardNameTextField;
@end
在我的.m文件中
@interface AddCardViewController ()
@property (nonatomic, strong)NSMutableArray *nameOfCards;
@end
@implementation AddCardViewController
@synthesize cardNameTextField = _cardNameTextField;
@synthesize nameOfCards = _nameOfCards;
- (NSMutableArray *)nameOfCards
{
if (!_nameOfCards)
_nameOfCards = [[NSMutableArray alloc] init];
return _nameOfCards;
}
- (IBAction)addNewCard:(id)sender {
[_nameOfCards addObject:self.cardNameTextField.text];
}
@end
答案 0 :(得分:1)
- (IBAction)addNewCard
{
[your_Array addObject:self.Your_textfeild.text];
}
数组应该是NSmutableArray。
答案 1 :(得分:1)
当您使用延迟加载时(即您在其getter方法中创建对象),您必须使用getter来访问该对象。不要通过其实例变量访问对象!
- (IBAction)addNewCard:(id)sender {
[self.nameOfCards addObject:self.cardNameTextField.text];
NSLog(@"my array content: %@", self.nameOfCards);
}
这就是为什么要用下划线为实例变量添加前缀。它告诉您不要直接使用实例变量,除非绝对必要。
答案 2 :(得分:1)
更改
- (IBAction)addNewCard:(id)sender {
[_nameOfCards addObject:self.cardNameTextField.text];
}
到
- (IBAction)addNewCard:(id)sender {
[self.nameOfCards addObject:self.cardNameTextField.text];
}
之后,你可以在NSLog中看到你的数组;
NSLog(@"%@", self.nameOfCards);
答案 3 :(得分:0)
使用 NSLog 查看数组的内容,即NSLog(@"%@",_nameOfCards);