在我的 ViewDidLoad 的 .m 文件中,我有以下代码:
if (![@"1" isEqualToString:[[NSUserDefaults standardUserDefaults] objectForKey:@"Name"]]) {
[[NSUserDefaults standardUserDefaults] setValue:@"1" forKey:@"Name"];
[[NSUserDefaults standardUserDefaults] synchronize];
UIAlertView *alertview = [[UIAlertView alloc] initWithTitle:@"What is your name?" message:@"\n\n\n" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Send", nil];
UITextField *utextfield = [[UITextField alloc] initWithFrame:CGRectMake (9.0, 60.0, 260.0, 25.0)]; utextfield.placeholder =@"Username";
[utextfield setBackgroundColor:[UIColor whiteColor]];
[alertview addSubview:utextfield];
[alertview show];
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
}
正如你所看到的,用户可以在这里保存他的名字,这样他就不必一遍又一遍地输入它。除此之外,他不会再被问到是否输入了它。但是,该应用程序在加载时不显示名称,而是显示数字 1。我想我在这里遗漏了明显的内容。