gpt4 book ai didi

ios - 以编程方式设置/激活 Segue

转载 作者:行者123 更新时间:2023-12-01 16:42:49 26 4
gpt4 key购买 nike

我正在尝试在我的 iOS 应用程序中设置首次启动 View 。为此,我有以下代码允许我在应用程序首次启动时执行代码。

我想要的是,当应用程序第一次启动时,用户被发送到我在 Storyboards 中设置的 FirstLaunch View ,而不是 FirstView View 。每次连续启动都会将用户发送到 FirstLaunch View 。

目前,我在 NavigationController.m 中执行此代码, 但如果在 AppDelegate.m 中执行得更好我可以改变它。

if ([[NSUserDefaults standardUserDefaults] boolForKey:@"FirstLaunch"])
{
// not first launch
}
else
{
// first launch

[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"FirstLaunch"];
[[NSUserDefaults standardUserDefaults] synchronize];
}

我的问题是: 如何以编程方式设置 segue(或激活 segue)以将用户发送到 FirstLaunch View 而不是普通的 FirstView?

最佳答案

你不需要一个 segue - 你所需要的只是一种以编程方式选择初始 View Controller 的方法,基于它是否是第一次启动。

为此,请取消选中 FirstView 上 Storyboard 中的“是初始 View Controller ”。 ,并按如下方式更改您的应用委托(delegate):

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
// Use the name of your storyboard below
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MyStoryboard" bundle:nil];
NSString *initialId;
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"FirstLaunch"]) {
// not first launch
initialId = @"FirstViewController";
} else {
// first launch
initialId = @"FirstLaunchViewController";
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"FirstLaunch"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
self.window.rootViewController = [storyboard instantiateViewControllerWithIdentifier:initialId];
[self.window makeKeyAndVisible];
return YES;
}

上面的代码嵌入了您的代码,该代码决定这是否是首次启动,并根据此决定设置目标屏幕的 Storyboard标识符。

请注意,此实现将永远不会再次向用户显示第一个启动屏幕,即使他在第一次出现时没有机会仔细查看它(例如,因为有人调用他,或者设备没电了力量)。更好的方法是阅读 NSUserDefaults和你做的一样,但是把它设置为 YES仅当用户关闭第一个启动 Controller 时。这样您就可以确定用户有足够的时间来查看屏幕并与之交互。

关于ios - 以编程方式设置/激活 Segue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22730148/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com