我有一个问题,我是iphone编程的首发,我不知道该怎么做。 我必须让KVO观察者知道文本字段何时接收字符串并将字符串显示到带有动画的tableview中,我已经从文本字段中获取了字符串数组但我不知道如何将其显示在tableview中。所以有些人会非常感谢!!
继承我的代码:
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
if (!self.tasks) self.tasks = [NSMutableArray new];
[self.tasks addObject:textField.text];
[userDefaults setObject:self.tasks forKey:@"tasks"];
NSLog(@"tasks:%@", [[NSUserDefaults standardUserDefaults]objectForKey:@"tasks"]);
NSLog(@"number of tasks:%d", self.tasks.count); <-- this print works good, also the first one
[textField addObserver:self forKeyPath:@"name" options:NSKeyValueObservingOptionNew context:NULL];
[textField resignFirstResponder];
return YES;
}
在tableviewcontroller中我只有这个(用于测试,但它不起作用:():
[self viewdidload];
if (!self.TasksArray) self.TasksArray = [NSMutableArray new];
self.TasksArray = [[NSUserDefaults standardUserDefaults]objectForKey:@"tasks"];
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSLog(@"number of tasks when tableview:%d", self.TasksArray.count);
//return self.TasksArray.count;
return self.TasksArray.count;
}
答案 0 :(得分:0)
将对象设置为[NSUserDefaults standardUserDefaults]
后
请务必致电[NSUserDefaults standardUserDefaults] synchronize];
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
if (!self.tasks) self.tasks = [NSMutableArray new];
[self.tasks addObject:textField.text];
[userDefaults setObject:self.tasks forKey:@"tasks"];
[userDefaults synchronize];
NSLog(@"tasks:%@", [[NSUserDefaults standardUserDefaults]objectForKey:@"tasks"]);
NSLog(@"number of tasks:%d", self.tasks.count); <-- this print works good, also the first one
[textField addObserver:self forKeyPath:@"name" options:NSKeyValueObservingOptionNew context:NULL];
[textField resignFirstResponder];
return YES;
}
在您将对象从[self.tableView reloadData]
分配到[NSUserDefaults standardUserDefaults]
后,您可能还需要致电self.TasksArray
。
- (void)viewDidLoad
{
[super viewDidload];
if (!self.TasksArray) self.TasksArray = [NSMutableArray new];
self.TasksArray = [[NSUserDefaults standardUserDefaults]objectForKey:@"tasks"];
[self.tableView reloadData];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSLog(@"number of tasks when tableview:%d", self.TasksArray.count);
return self.TasksArray.count;
}
根据您的应用架构,最好在[self.tableView reloadData]
viewWillAppear:(BOOL)animated
答案 1 :(得分:0)
尝试使用textFieldDidEditing下面的代码。
- (void)textFieldDidEndEditing:(UITextField *)textField
{
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
if (!self.tasks) self.tasks = [NSMutableArray new];
[self.tasks addObject:textField.text];
[userDefaults setObject:self.tasks forKey:@"tasks"];
[userDefaults synchronize];
NSLog(@"tasks:%@", [[NSUserDefaults standardUserDefaults]objectForKey:@"tasks"]);
NSLog(@"number of tasks:%d", self.tasks.count); <-- this print works good, also the first one
[textField addObserver:self forKeyPath:@"name" options:NSKeyValueObservingOptionNew context:NULL];
[textField resignFirstResponder];
[self.yourtableview reloadData];
}
希望它可以帮助你..