xcode 如何仅在首次启动时显示视图?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12878162/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 01:49:06  来源:igfitidea点击:

How can I show a view on the first launch only?

iosxcode

提问by jac300

I built an application using Xcode 4.5 with storyboards. The first time the app launches I want the initial view controller to appear with terms and conditions that must be accepted to proceed. After that, I want the app to launch and skip over the first view controller and go to the second one.

我使用 Xcode 4.5 和故事板构建了一个应用程序。应用程序第一次启动时,我希望初始视图控制器显示必须接受的条款和条件才能继续。之后,我希望应用程序启动并跳过第一个视图控制器并转到第二个视图控制器。

I know I have to use the NSUserDefaults class and something to the effect of: if ([[NSUserDefaults standard...] boolForKey:@"iHaveAcceptedTheTerms"])

我知道我必须使用 NSUserDefaults 类和一些效果: if ([[NSUserDefaults standard...] boolForKey:@"iHaveAcceptedTheTerms"])

But I have never used this class before and have no idea how to implement this code. Can someone share the specifics of how to do this?

但是我以前从未使用过这个类,也不知道如何实现这段代码。有人可以分享如何做到这一点的具体细节吗?

采纳答案by NightCoder

You put in in your AppDelegate:

你在你的 AppDelegate 中输入:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {  

//first-time ever defaults check and set
if([[NSUserDefaults standardUserDefaults] boolForKey:@"TermsAccepted"]!=YES)
{
    [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"TermsAccepted"];
}

Then you implement in your rootViewController the terms and conditions and a way to accept it. You will have to check if the terms are accepted, for example like this:

然后在 rootViewController 中实现条款和条件以及接受它的方式。您必须检查条款是否被接受,例如:

if ([[NSUserDefaults standardUserDefaults] boolForKey:@"TermsAccepted"]){
    //proceed with app normally
}
else{
//show terms
}

When accepted, the following code will change the default settings:

接受后,以下代码将更改默认设置:

 if(termsaccepted){
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"TermsAccepted"];
}

回答by Loic Verrall

In the interests of keeping this question up-to-date, here is a Swiftversion of the accepted answer.

为了使这个问题保持最新状态,这里是已接受答案的Swift版本。



STEP 1

第1步

In your App Delegate, add the following function.

在您的App Delegate 中,添加以下函数。

func applicationDidFinishLaunching(application: UIApplication) {
    if !NSUserDefaults.standardUserDefaults().boolForKey("TermsAccepted") {
        NSUserDefaults.standardUserDefaults().setBool(false, forKey: "TermsAccepted")
    }
} 

This will essentially set your TermsAccepted Bool to false if this is the first launch (as Bools are falseby default).

如果这是第一次启动,这将从本质上将您的 TermsAccepted Bool 设置为 false(因为 Bools默认为false)。



STEP 2

第2步

In your root view controller (the view controller which loads when your app is launched), you must have a way to see if the terms have been accepted or not and act accordingly.

在您的根视图控制器(在您的应用程序启动时加载的视图控制器)中,您必须有办法查看条款是否已被接受并采取相应的行动。

Add the following function.

添加以下功能。

override func viewDidAppear(animated: Bool) {
    if NSUserDefaults.standardUserDefaults().boolForKey("TermsAccepted") {
        // Terms have been accepted, proceed as normal
    } else {
        // Terms have not been accepted. Show terms (perhaps using performSegueWithIdentifier)
    }
}


STEP 3

第 3 步

Once the user accepts your conditions, you want to change your TermsAcceptedBool to true. So in the body of the method which handles the acceptance of the terms, add the following line.

一旦用户接受您的条件,您希望将您的TermsAcceptedBool更改为true。因此,在处理接受条款的方法的主体中,添加以下行。

NSUserDefaults.standardUserDefaults().setBool(true, forKey: "TermsAccepted")


I hope this helps!

我希望这有帮助!

Loic

洛伊克

回答by Jessie

Swift 3 Version

斯威夫特 3 版本

In AppDelegate.swift:

在 AppDelegate.swift 中:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    if !UserDefaults.standard.bool(forKey: "Walkthrough") {
        UserDefaults.standard.set(false, forKey: "Walkthrough")
    }
}

In root view controller:

在根视图控制器中:

override func viewDidLoad() {
    super.viewDidLoad()
    if UserDefaults.standard.bool(forKey: "Walkthrough") {
        // Terms have been accepted, proceed as normal            
    } else {
        // Terms have not been accepted. Show terms (perhaps using 
    }
}

When terms have been accepted, or tutorial walkthrough is finished:

接受条款或完成教程演练后:

UserDefaults.standard.set(true, forKey: "Walkthrough")

回答by alexandresoli

It will look like this on your first view or delegate:

在您的第一个视图或委托中,它看起来像这样:

NSUserDefaults * standardUserDefaults = [NSUserDefaults standardUserDefaults];

BOOL isAccepted = [standardUserDefaults boolForKey:@"iHaveAcceptedTheTerms"];

if (!isAccepted) {
    [self presentViewController:YOUR_TERMS_CONTROLLER animated:YES completion:nil];
} else {
    [self.navigationController pushViewController:YOUR_NORMAL_CONTROLLER animated:YES];
}

Dont forget to save the user response on your terms controller:

不要忘记将用户响应保存在您的条款控制器上:

[standardUserDefaults setBool:YES forKey:@"iHaveAcceptedTheTerms"];