1

我正在尝试放置第一个启动视图。我已经尝试了一些东西,但这不起作用。

这是我所拥有的:

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

    UIPageControl *pageControl = [UIPageControl appearance];
    pageControl.pageIndicatorTintColor = [UIColor lightGrayColor];
    pageControl.currentPageIndicatorTintColor = [UIColor blackColor];
    pageControl.backgroundColor = [UIColor whiteColor];

    return YES;

    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"yourCondition"])
    {
        //launch your first time view
        self.viewController = [[viewController alloc] initWithNibName:@"viewController" bundle:nil];
    }
    else
    {
        //launch your default view
        self.viewController = [[viewController alloc] initWithNibName:@"defaultViewController" bundle:nil];

        [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"yourCondition"];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }

}

不知何故,它说 viewController 不是 AppDelegate 的对象类型。有任何想法吗?

4

3 回答 3

3

如果你什么都不做,故事板的初始视图控制器就会出现。如果你想让其他事情发生,你想要设置的是self.window.rootViewController. 在这种情况下,您还需要手动创建和显示窗口。

于 2014-05-20T17:53:53.430 回答
0

您的实施存在一些问题,application:didFinishLaunchingWithOptions:这绝对是您遇到麻烦的原因。

我建议您阅读一些有关 iOS 开发入门的教程,因为如果您打算在未来进行任何认真的开发,这些都是您需要掌握的基本概念。

话虽这么说,下面的代码应该是正确的:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [self setWindow:[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]];

    UIViewController *viewController = nil;

    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"yourCondition"])
    {
        //launch your first time view
        viewController = [[viewController alloc] initWithNibName:@"viewController" bundle:nil];
    }
    else
    {
        //launch your default view
        viewController = [[viewController alloc] initWithNibName:@"defaultViewController" bundle:nil];

        [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"yourCondition"];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }

    [[self window] setRootViewController:viewController];

    [[self window] makeKeyAndVisible];

    return YES;
}

的默认实现application:didFinishLaunchingWithOptions:期望您返回一个布尔值,由方法签名指示。

您还错过了方法开头的重要调用,该方法NSWindow为要呈现的应用程序设置 main。

正如@matt 所指出的,您的代码希望您在 AppDelegate 上有一个属性,该属性将保存您对viewController. 从您提到的编译错误来看,您似乎没有这种关系设置。rootViewController碰巧的是,您可能无论如何都不需要这个,因为您可以通过设置NSWindow.

同样,我建议您阅读一些 iOS 开发初学者教程或购买一本像样的书,因为此类问题相当基础。如果您独自在这方面遇到麻烦,您只会发现自己更加困惑,并且可能需要您开始研究更复杂代码的帮助。

于 2014-05-20T18:17:24.747 回答
0

我在 Swift 中有解决方案,并在 GitHub 上发布了一个示例来分享:

https://github.com/AppLogics/DynamicEntryPointExample-iOS

我将在这里解释它的简化版本,以动态/以编程方式在蓝色红色视图控制器之间设置入口点。

确保在界面生成器中从情节提要中删除所有入口点,为此,您需要选择ViewControllerEntry Point取消选中该部分Is Initial View Controller下的选项View Controller

在此处输入图像描述

然后,您需要确保所有潜在的入口点都已分配Storyboard Id,因为您将在AppDelegate.swift文件中的代码中使用它。此示例使用bluered

在此处输入图像描述

接下来,您必须编辑该AppDelegate.swift文件。首先声明要测试的常量并决定最初显示哪个 View Controller,在这种情况下:

let blueEntryPoint = true

didFinishLaunchingWithOptions接下来在注释:// Override point for customization after application launch.和语句之间的默认函数中插入一些代码:return true像这样:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.

    //checking to see if the blue is true or false
    if blueEntryPoint {
        //Showing blue View Controller
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let mainViewController = storyboard.instantiateViewController(withIdentifier: "blue")
        self.window = UIWindow(frame: UIScreen.main.bounds)
        self.window?.rootViewController = mainViewController
        self.window?.makeKeyAndVisible()
    } else {
        //Not showing blue, i.e. will show red View Controller
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let surveyViewController = storyboard.instantiateViewController(withIdentifier: "red")
        self.window = UIWindow(frame: UIScreen.main.bounds)
        self.window?.rootViewController = surveyViewController
        self.window?.makeKeyAndVisible()
    }

    return true
}

运行这个,然后更改blueEntryPointfalse并重新运行它!

于 2018-11-02T03:18:30.683 回答