我在VC中有textview。使用nsuserdefaults保存此textview并稍后获取。在VC1中我得到了保存的textview并在UITableView中显示。但是当我启动应用程序时,它会自动在索引0中显示“null”文本。
VC:
-(void)save:(id)sender{
NSUserDefaults *userData1 = [NSUserDefaults standardUserDefaults];
[userData1 setObject:textView.text forKey:@"savetext"];
[userData1 synchronize];
}
VC1:
-(void) viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
// textArray=[[NSMutableArray alloc]init];
txt=[[UITextView alloc]initWithFrame:CGRectMake(0, 0, 320, 400)];
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
// getting an NSString
NSString *savedValue = [prefs stringForKey:@"savetext"];
NSLog(@"saved is %@",savedValue);
txt.text = [NSString stringWithFormat:@"%@", savedValue];
NSLog(@"text.txt is %@", txt.text);
MyAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
if(![MyAppDelegate.textArray containsObject:txt.text]){
[MyAppDelegate.textArray addObject:txt.text];
}
NSUserDefaults *userData1 = [NSUserDefaults standardUserDefaults];
[userData1 setObject:MyAppDelegate.textArray forKey:@"save"];
[userData1 synchronize];
}
UITableView使用数组显示文本值:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSMutableArray* myMutableArrayAgain = [NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"save"]];
NSLog(@"array is %@",myMutableArrayAgain);
return [myMutableArrayAgain count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSMutableArray* myMutableArrayAgain = [NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"save"]];
NSLog(@"mycell is %@",myMutableArrayAgain);
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
NSLog(@"cell is %@",cell);
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
NSLog(@"cell inside is %@",cell);
}
// Configure the cell...
cell.textLabel.text = [myMutableArrayAgain objectAtIndex:indexPath.row];
[cell.textLabel setFont:[UIFont fontWithName:@"Arial-BoldMT" size:14]];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
答案 0 :(得分:0)
这都是因为某些地方的代码错误:
1)带有数组的Nsmutable数组是错误的: - 因为userdefaults返回一个字符串。 所以这样做: -
NSUserDefaults *userData1 = [NSUserDefaults standardUserDefaults];
NSMutableArray* myMutableArrayAgain = [NSMutableArray arrayWithObject:[userData1 valueForKey:@"savetext"]];
2)在VC中,你在“savetext”中保存key的值,但是在VC1中你要将值检索为“save”,所以改变它,它已在上面的代码中完成。
//以上2点可以解决你的问题,但我会说不要这样编码,因为它会消耗更多的内存,并会删除代码的整洁。您所做的代码可以非常有效的方式完成。 我告诉你 。
//首先使用View控制器VC并将IBAction添加到按钮和textView或textField中。 在VC中写这段代码: -
.h文件
@interface demoViewController : UIViewController{
IBOutlet UITextField *txt1;
}
-(IBAction)getData:(id)sender;
.m文件
//首先包含您要重定向的下一个控制器的头文件,假设XYZ
#import XYZ.h
-(IBAction)getData:(id)sender{
NSUserDefaults *userData1 = [NSUserDefaults standardUserDefaults];
[userData1 setObject:txt1.text forKey:@"savetext"];
demoViewController1 *demo=[[demoViewController1 alloc]initWithNibName:@"demoViewController1" bundle:nil];
[self.navigationController pushViewController:demo animated:NO];
}
//在ViewDisLoad或ViewDidAppear上选择第二个控制器IN taht
//获取全局MuttableArray
NSMutableArray *myMutableArrayAgain;
//我在ViewDidload上编写此代码,在此处分配数组,因为需要分配MutableObject
NSUserDefaults *userData1 = [NSUserDefaults standardUserDefaults];
myMutableArrayAgain=[[NSMutableArray alloc]init];
[myMutableArrayAgain addObject: [userData1 valueForKey:@"savetext"]];
//现在查看表格方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [myMutableArrayAgain count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
NSLog(@"cell is %@",cell);
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
NSLog(@"cell inside is %@",cell);
}
// Configure the cell...
cell.textLabel.text = [myMutableArrayAgain objectAtIndex:indexPath.row];
[cell.textLabel setFont:[UIFont fontWithName:@"Arial-BoldMT" size:14]];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
//以这种方式应用,您可以用更少的代码和更好的方式完成这些工作。