NSUserDefaults的问题

时间:2014-01-02 03:11:10

标签: 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 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];
}

在ViewController B中:

// ViewDidLoad

self.aLabel.text = [[NSUserDefaults standardUserDefaults] stringForKey:@"stringKey"];

在我的视图控制器A中,我在NSUserDefaults中设置了字符串,我可以看到它与我点击的单元格相匹配,但是当我推送到View Controller B时,我的标签没有被选中完成NSUserDefaults中存储的内容。

我错过了什么?

修改

我把它更改为:

// 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 个答案:

答案 0 :(得分:0)

1,确保字符串有效,这意味着它既不是@“”也不是“白色字符”。

2,确保将self.aLabel添加到基本视图中并且self.aLabel不是nil。