Ios 与NSUserDefaults有关的问题
在ViewController A中:Ios 与NSUserDefaults有关的问题,ios,objective-c,uitableview,uilabel,nsuserdefaults,Ios,Objective C,Uitableview,Uilabel,Nsuserdefaults,在ViewController A中: // ViewDidLoad self.theArray = // set up my array here with objects // TableView - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { self.viewControllerTwo = [[SecondViewController
// ViewDidLoad
self.theArray = // set up my array here with objects
// TableView
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
self.viewControllerTwo = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
self.myString = [self.theArray objectAtIndex:indexPath.row];
[[NSUserDefaults standardUserDefaults] setValue:self.myString forKey:@"stringKey"];
[[NSUserDefaults standardUserDefaults] synchronize];
[self.navigationController pushViewController:self.viewControllerTwo animated:YES];
}
// ViewDidLoad
self.aLabel.text = [[NSUserDefaults standardUserDefaults] stringForKey:@"stringKey"];
在ViewController B中:
// ViewDidLoad
self.theArray = // set up my array here with objects
// TableView
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
self.viewControllerTwo = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
self.myString = [self.theArray objectAtIndex:indexPath.row];
[[NSUserDefaults standardUserDefaults] setValue:self.myString forKey:@"stringKey"];
[[NSUserDefaults standardUserDefaults] synchronize];
[self.navigationController pushViewController:self.viewControllerTwo animated:YES];
}
// ViewDidLoad
self.aLabel.text = [[NSUserDefaults standardUserDefaults] stringForKey:@"stringKey"];
在我的视图控制器A中,我在NSUserDefaults
中设置字符串,我可以看到它与我点击的单元格相匹配,但当我按下视图控制器B时,我的标签没有拾取存储在NSUserDefaults
中的内容
我错过了什么
编辑:
// ViewDidLoad
self.theArray = // set up my array here with objects
// TableView
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
self.viewControllerTwo = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
self.myString = [self.theArray objectAtIndex:indexPath.row];
[[NSUserDefaults standardUserDefaults] setValue:self.myString forKey:@"stringKey"];
[[NSUserDefaults standardUserDefaults] synchronize];
[self.navigationController pushViewController:self.viewControllerTwo animated:YES];
}
// ViewDidLoad
self.aLabel.text = [[NSUserDefaults standardUserDefaults] stringForKey:@"stringKey"];
我把它改成这样:
// In FirstViewController
self.viewControllerTwo = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
self.myString = [self.theArray objectAtIndex:indexPath.row];
self.viewControllerTwo.secondString = self.myString;
[self.navigationController pushViewController:self.viewControllerTwo animated:YES];
// In SecondViewController
self.aLabel.text = self.secondString;
1、 确保字符串有效,这意味着它既不是@字符,也不是“白色字符”
2、 确保将self.aLabel添加到基本视图中,并且self.aLabel不是nil。字符串有效,它将获取我设置的值。self.aLabel返回为(null)though@LukeIrvin这就是问题所在。尝试在NSLog中显示,看看它是否出现在日志中。如果出现,则标签中存在问题,而不是userdefaultsI中。我刚让它工作起来。我没有使用NSUserDefaults,而是在SecondViewController中设置一个字符串以匹配FirstViewController中的字符串,然后将标签文本设置为字符串值。