我是新来的,是Xcode的初学者,我正在尝试用5页的应用程序。第一个是带有4个按钮的菜单页面,每个按钮通向不同的页面。首次启动应用程序时,您可以选择要转到的页面(来自其他4页)。我想要做的是让应用程序在第二次启动时跳过菜单页面(直接转到之前选择的页面)。任何人都可以给我一些关于在哪里找到有关这方面的提示的指示,或者举一个与此相关的例子吗?
答案 0 :(得分:0)
当用户选择页面时,我会将该选择存储到NSUserDefaults
...
[[NSUserDefaults standardUserDefaults] setObject:chosenPageIndex forKey:@"selectedPage"];
(chosenPageIndex是页面索引的NSNumber)。
然后,当应用程序启动时,您可以执行此操作...
NSNumber *prevSelectedPage = [[NSUserDefaults standardUserDefaults] objectForKey:@"selectedPage"];
if (prevSelectedPage != nil) {
//use this bit to move to the selected page using the page number stored in prevSelectedPage
}
多数民众赞成我会怎么做。
答案 1 :(得分:0)
试试这个:
-(void)viewDidLoad{
[super viewDidLoad];
if ([[NSUserDefaults standardUserDefaults]boolForKey:@"secondTime"]==YES) {
switch ([[NSUserDefaults standardUserDefaults]integerForKey:@"nextTime"]) {
case 1:
[self.navigationController pushViewController:firstViewController animated:YES];
break;
case 2:
[self.navigationController pushViewController:secondViewController animated:YES];
break;
case 3:
[self.navigationController pushViewController:thirdViewController animated:YES];
break;
case 4:
[self.navigationController pushViewController:forthViewController animated:YES];
break;
default:
break;
}
return;
}
}
-(void)btnClicked:(id)sender{
[[NSUserDefaults standardUserDefaults]setInteger:[sender tag] forKey:@"nextTime"];
[[NSUserDefaults standardUserDefaults]setBool:YES forKey:@"secondTime"];
[[NSUserDefaults standardUserDefaults]synchronize];
switch ([sender tag]) {
case 1:
[self.navigationController pushViewController:firstViewController animated:YES];
break;
case 2:
[self.navigationController pushViewController:secondViewController animated:YES];
break;
case 3:
[self.navigationController pushViewController:thirdViewController animated:YES];
break;
case 4:
[self.navigationController pushViewController:forthViewController animated:YES];
break;
default:
break;
}
答案 2 :(得分:0)
您可以将NSUserDefault用于此目的。
当用户选择一个选项时,将其添加到NSUSerDefaults
,如:
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[NSNumber numberWithInt:selectedPage] forKey:@"Selected_Option"];
[defaults synchronize];
当用户再次打开应用时,在applicationWillLaunch
appdelegate方法中,您可以检索以下数据:
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
NSNumber *page = [defaults objectForKey:@"Selected_Option"];
int selected = [page intValue];
根据所选页面执行您想要的操作。首次加载时,所选的将等于0.