简体   繁体   English

在iOS中仅显示UIView的应用程序演示文稿一次

[英]Show UIView for app presentation only once in iOS

I need to show an UIScrollView with some pages for present the app to user when launched for the first time. 我需要显示一个带有一些页面的UIScrollView,以便在第一次启动时向用户显示该应用程序。 My slides are working correctly, lefting the logic to present it. 我的幻灯片正常工作,提供逻辑来呈现它。 Is a good practice to set the UIScrollViewController as root controller and set a NSUserDefault variable to control the presentation and redirect to main controller or set the main ViewController as root normally and call the presentation view if needed is a better approach? 将UIScrollViewController设置为根控制器并设置NSUserDefault变量以控制表示并重定向到主控制器或将主ViewController正常设置为root并在需要时调用表示视图是更好的方法是一个好习惯吗? If there is a better way to do this can someone help? 如果有更好的方法可以帮到别人吗? Thanks. 谢谢。

The default behavior of your app is to not present the introduction slides. 您的应用的默认行为是不提供介绍幻灯片。 They are presented only the first time you launch it. 它们仅在您第一次启动时呈现。

To make the decision of presenting those slides, you ask the question 要做出提交这些幻灯片的决定,请提出问题

Is it the first time the app is launched ? 这是应用程序首次推出吗?

Where should this question belong ? 这个问题应该在哪里?

If you put it in your introduction view controller, you will not be able to present it from a button somewhere else in your app (ie a button tutorial or something). 如果您将它放在简介视图控制器中,您将无法通过应用程序中的其他位置(即按钮tutorial或其他内容)中的按钮显示它。 By putting the presentation logic outside of your view controller, you leave it with only the responsibility of managing its content, and get better separation of concerns. 通过将表示逻辑放在视图控制器之外,您只需要负责管理其内容,并更好地分离关注点。

Counter example 反例

If you had a login view controller, that takes care of logging in the user. 如果您有登录视图控制器,则负责登录用户。 A way to think about this behavior would be the user logs in once, and then I want to skip this view controller every time, and so the logic of presenting it should belong in another view controller. 考虑这种行为的方法是用户登录一次,然后我想每次都跳过这个视图控制器,因此呈现它的逻辑应该属于另一个视图控制器。 But in that case, logging in will probably have consequences on the rest of your app (setting the Authorization header of you networking library for exemple). 但在这种情况下,登录可能会对您的应用程序的其余部分产生影响(例如,为您的网络库设置Authorization标头)。 In this case I would set it as the first view controller of the app but skip the UI if we have an auth-token stored in the user defaults, therefore following the former proposition you made. 在这种情况下,我会将其设置为应用程序的第一个视图控制器,但如果我们在用户默认值中存储了auth-token,则跳过UI,因此遵循您提出的前一个命题。 Futhermore, in the context of a navigation controller, it makes sense having this view controller as the root, as you will easily be able to popToRootViewController if your user ever needs to log-out (or the session token expires) 此外,在导航控制器的上下文中,将此视图控制器作为根是有意义的,因为如果用户需要注销(或会话令牌过期),您将能够轻松地使用popToRootViewController

I suggest setting your main view as the default, since 99.99% of the time this is what will be used. 我建议将主视图设置为默认视图,因为这将是99.99%的时间。 Use NSUserDefault to set a bool if this was shown, maybe didShowFirstRun. 如果显示,则使用NSUserDefault设置bool,可能是didShowFirstRun。 Then in your main controller, check what the value of that NSUserDefault and show if its nil or false. 然后在主控制器中,检查NSUserDefault的值,并显示其是否为零。

In your AppDelegate in the application didFinishLaunchingWithOptions you could check if didShowFirstRun is nil and set a default value to NO. 在应用程序didFinishLaunchingWithOptions的AppDelegate中,您可以检查didShowFirstRun是否为nil并将默认值设置为NO。

Don't design around the exception to the app workflow, showing a first run would be the exception. 不要围绕应用程序工作流的异常进行设计,显示第一次运行将是例外。

I have an app that after the login view is shown, it checks for first run and show the getting started screen, then after that has completed, sets the NSUserDefault var and I am done. 我有一个应用程序,在显示登录视图后,它会检查首次运行并显示启动屏幕,然后在完成后,设置NSUserDefault var并完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM