仅在 iOS 中首次启动时显示屏幕
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9450391/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Show screen on first launch only in iOS
提问by Frenck
Tweetbot and Clear show's on the first start of the app a small tutorial screen how the app works. The screen with the small tutorial only pops up on the first start up of the app (1 time)
Tweetbot 和 Clear 在应用程序的第一次启动时显示一个小教程屏幕应用程序的工作原理。带有小教程的屏幕仅在应用程序第一次启动时弹出(1 次)
How and with what can i make a similar thing? Can anyone push me in the right direction?
我如何以及用什么可以做出类似的事情?谁能把我推向正确的方向?
View i mean:
我的意思是:
回答by chroipahtz
I'm assuming by Xcode you actually mean iOS.
我假设 Xcode 您实际上是指 iOS。
What you need to do is use the NSUserDefaults
class to store a flag indicating whether the user has seen the tutorial screen before.
您需要做的是使用NSUserDefaults
该类来存储一个标志,该标志指示用户之前是否看过教程屏幕。
When your app first loads (or at the point you want to decide whether or not to show the tutorial screen), do something like this:
当您的应用程序首次加载时(或者您想决定是否显示教程屏幕时),请执行以下操作:
if(![[NSUserDefaults standardUserDefaults] boolForKey:@"hasSeenTutorial"])
[self displayTutorial];
This checks the saved NSUserDefaults for the current user for a value named "hasSeenTutorial", which won't exist yet. Since it doesn't exist, it will call displayTutorial
. displayTutorial
refers to your method for creating the tutorial view. You can figure out that part.
这会检查为当前用户保存的 NSUserDefaults 中名为“hasSeenTutorial”的值,该值尚不存在。由于它不存在,它将调用displayTutorial
. displayTutorial
指的是您创建教程视图的方法。你可以弄清楚那部分。
Then, once the user closes the tutorial screen:
然后,一旦用户关闭教程屏幕:
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"hasSeenTutorial"];
That value will be saved for your user profile, meaning the next time it checks it, it will be true, so displayTutorial
won't be called.
该值将为您的用户配置文件保存,这意味着下次检查它时,它将为真,因此displayTutorial
不会被调用。
回答by Blazer
In your viewDidLoad:
在您的 viewDidLoad 中:
if (![@"1" isEqualToString:[[NSUserDefaults standardUserDefaults]
objectForKey:@"aValue"]]) {
[[NSUserDefaults standardUserDefaults] setValue:@"1" forKey:@"aValue"];
[[NSUserDefaults standardUserDefaults] synchronize];
//Action here
}
回答by Dmitry Varavkin
Certainly, if we would like to tell user something about features after update (not only app launched first time), the solution below could be suitable.
当然,如果我们想在更新后告诉用户一些关于功能的信息(不仅仅是第一次启动的应用程序),下面的解决方案可能是合适的。
In your viewDidLoad:
在您的 viewDidLoad 中:
NSString *currentBundleVersion = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"];
NSString *previousBundleVersion = [[NSUserDefaults standardUserDefaults] objectForKey:@"PreviousBundleVersion"];
if (![currentBundleVersion isEqualToString:previousBundleVersion] ) {
// Here you can initialize your introduction view and present it!
}
Once the user closes the intro:
一旦用户关闭介绍:
NSString *currentBundleVersion = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"];
NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
if (standardUserDefaults) {
[standardUserDefaults setObject:currentBundleVersion forKey:@"PreviousBundleVersion"];
[standardUserDefaults synchronize];
}
In this case app bundle version stored in your standardUserDefaults will be differ from current bundle version only after update and shown only once as well as at first launch.
在这种情况下,存储在您的标准用户默认值中的应用程序包版本将仅在更新后与当前的包版本不同,并且仅在首次启动时显示一次。
回答by Abizern
Initialise your user defaults with a BOOL, something called instructionsSeen
(or whatever you want) and set it to NO
in your App delegate's initialize method.. In your app, test this value and if it is NO
display your tutorial screen. As part of showing and displaying this screen, set the instructionsSeen
to YES
and store it in your defaults.
使用 BOOL 初始化您的用户默认值,称为instructionsSeen
(或任何您想要的)并将其设置为NO
您的 App 委托的 initialize 方法.. 在您的应用程序中,测试此值,如果它NO
显示您的教程屏幕。作为显示和显示此屏幕的一部分,将 设置instructionsSeen
为YES
并将其存储在您的默认值中。
This way the demo screen will only show on first launch, unless the user uninstalls and installs the app again.
这样演示屏幕只会在第一次启动时显示,除非用户卸载并再次安装应用程序。
You could also show the demo for a small number of launches (say 3). In this case, don't use BOOL
use a number and increment it instead.
您还可以展示少量启动的演示(比如 3)。在这种情况下,不要使用BOOL
数字而是将其递增。
回答by pkozlowski
Xamarin.iOS Version within AppDelegate:
AppDelegate 中的 Xamarin.iOS 版本:
UIStoryboard storyboard = UIStoryboard.FromName("Main", null);
if (NSUserDefaults.StandardUserDefaults.BoolForKey ("hasSeenTutorial") == false) {
UIViewController vc = storyboard.InstantiateViewController ("StartPageViewController");
this.Window.RootViewController = vc;
} else {
UIViewController vc = storyboard.InstantiateViewController ("NonStartPageViewController");
this.Window.RootViewController = vc;
}
this.Window.MakeKeyAndVisible ();
In my StartPageViewController, I have a button which sets NSUserDefaults to true, so the next time it runs, it will start off with the NonStartPageViewController:
在我的 StartPageViewController 中,我有一个将 NSUserDefaults 设置为 true 的按钮,因此下次运行时,它将以 NonStartPageViewController 开始:
partial void RegisterButton_TouchUpInside (UIButton sender)
{
NSUserDefaults.StandardUserDefaults.SetBool(true,"hasSeenTutorial");
NSUserDefaults.StandardUserDefaults.Synchronize();
}
回答by pkozlowski
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if (![defaults objectForKey:@"firstRun"]) {
[defaults setObject:[NSDate date] forKey:@"firstRun"];
[self displayTutorial];
}
回答by Jeff
Everyone is making this more complex and vauge than it needs to be... Simple complete solution.
每个人都在使这变得比它需要的更复杂和虚幻……简单的完整解决方案。
In the ViewDidLoad:
在 ViewDidLoad 中:
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"FirstLoadKey"]) {
self.imageView.hidden = YES;
}else{
self.imageView.hidden = NO;
[[NSUserDefaults standardUserDefaults]setBool:YES forKey:@"FirstLoadKey"];
[[NSUserDefaults standardUserDefaults]synchronize];
}
回答by PoolHallJunkie
Swift version :
斯威夫特版:
if !(NSUserDefaults.standardUserDefaults().boolForKey("seenTutorial")) {
//Tutorial part
NSUserDefaults.standardUserDefaults().setBool(true, forKey: "seenTutorial")
}