Objective-C:获取应用程序启动时的用户信息
当我的应用程序启动时,我需要获取用户的性别。
如何仅在应用程序首次启动时调用该代码?我只想显示一个带有简单“你是男性还是女性?”的警报视图。然后存储选择以供应用程序的其余部分使用。我不希望用户每次打开应用程序时都被询问,只是在第一次打开它时被询问。
谢谢,
杰克
编辑
我知道如何实际存储数据,但是我在哪里调用代码以便仅在应用程序加载时显示警报视图?
I need to get the users sex when my application starts up.
How do I call the code only when the application first starts up? I just want to display an alert view with a simple "Are you Male or Female?" and then store the selection for use throughout the rest of the application. I don't want the user to be asked every time they open the app, just the first time they open it.
Thanks,
Jack
EDIT
I know how to actually store the data, but where do I call the code so the alert view is only displayed when the application loads up?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
编辑
你可以放置它,以便在听起来
像是
NSUserDefaults
的一个很好的用途。这取决于您想要如何存储该信息,例如,如果我要使用字符串(我选择字符串是因为检测是否已设置值比使用 int 更容易)我可以有类似的东西然后,当用户选择他们的性别时,您可以使用以下内容保存它:
Edit
You could place this so that is get's called in
Sounds like a good use for
NSUserDefaults
. It depends how you want to store that information for example if I was to use a string (I've chosen string as detecting whether no value is already set is easier than usingint
) I could have something like this on start upThen when a user has selected their sex you can save it with something like:
无论您使用 Paul.s 还是 Regexident 的答案,将代码放在哪里并不重要。它仍然只会执行一次。
但是,如果您询问要将其放在哪里以便在应用程序启动后立即显示,则需要将其放在应用程序的第一个视图控制器的 viewDidLoad 中(可能在最后)呈现给用户。
Whether you use Paul.s or Regexident's answers, it doesn't matter too much where you put the code. It'll still only be executed once.
But if you're asking where you want to put it so that it shows up immediately after the app launches, you want to put it in
viewDidLoad
(probably at the end) for the first view controller your app presents to the user.