简体   繁体   English

iOS First Launch视图

[英]iOS First Launch view

I am trying to put a first launch view. 我想尝试第一个启动视图。 I already tried some stuff but that won't work. 我已经尝试了一些东西,但那不行。

Here is what I have: 这是我有的:

- (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];
    }

}

Somehow that doesn't work it says that viewController is not found as an object type of AppDelegate. 不知何故,它不起作用,它说找不到viewController作为AppDelegate的对象类型。 Any Ideas? 有任何想法吗?

If you do nothing, the storyboard's initial view controller will appear. 如果您不执行任何操作,则会显示故事板的初始视图控制器。 If you want something else to happen, what you want to set is self.window.rootViewController . 如果你想要其他东西发生,你想要设置的是self.window.rootViewController In that case you will also need to create and show the window manually. 在这种情况下,您还需要手动创建和显示窗口。

There are a few things wrong with your implementation of application:didFinishLaunchingWithOptions: which is most definitely the cause of your troubles. 你的application:didFinishLaunchingWithOptions:实现有一些问题application:didFinishLaunchingWithOptions:这肯定是你的麻烦的原因。

I recommend that you read through a few tutorials on getting started with iOS development as these are fundamental concepts you will need to grasp if you intend on doing any serious development in the future. 我建议您阅读一些关于iOS开发入门的教程,因为如果您打算在未来进行任何认真的开发,这些是您需要掌握的基本概念。

That being said, the following code should see you right: 话虽如此,以下代码应该看对你:

- (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;
}

The default implementation of application:didFinishLaunchingWithOptions: expects that you return a boolean, which is indicated by the method signature. application:didFinishLaunchingWithOptions:的默认实现application:didFinishLaunchingWithOptions:期望返回一个布尔值,由方法签名指示。

You are also missing the important call at the beginning of the method which sets up the main NSWindow for your application to be presented. 您还错过了方法开头的重要调用,该方法为您的应用程序设置了主要的NSWindow

As pointed out by @matt, your code expects that you have a property on you AppDelegate which would hold your reference to viewController . 正如@matt所指出的,你的代码希望你在AppDelegate上有一个属性,它可以保存你对viewController的引用。 From what you mentioned about the compilation error, it would seem that you do not have this relationship setup. 根据您提到的有关编译错误的内容,您似乎没有设置此关系。 As it happens, you might not need this anyway as you can essentially achieve the same thing by setting the rootViewController property of your NSWindow . 实际上,您可能不需要这样,因为您可以通过设置NSWindowrootViewController属性来实现相同的rootViewController

Again, I would recommend that you read up on some beginner tutorials for iOS development or purchase a decent book as issues like this are fairly fundamental. 同样,我建议你阅读一些关于iOS开发的初学者教程或购买一本像样的书,因为这样的问题是相当基础的。 If you're having troubles with this alone, you will only find yourself more confused and likely to require assistance one you start delving into more complex code. 如果你单独遇到麻烦,你只会发现自己更加困惑,并且可能需要帮助才能开始钻研更复杂的代码。

I have the solution in Swift and have published an example on GitHub to share: 我在Swift中有解决方案并在GitHub上发布了一个例子来分享:

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

I will explain here with a simplified version of it to dynamically/programmatically set the Entry Point from between a blue and a red View Controller. 我将在这里解释它的简化版本,以动态/编程方式设置蓝色红色视图控制器之间的入口点。

Ensure in interface builder you remove all entry points from your storyboard, in order to do this you will need to select the ViewController with the Entry Point and uncheck the Is Initial View Controller option under the View Controller section: 确保在界面构建器中删除故事板中的所有入口点,为此,您需要选择带Entry PointViewController并取消选中View Controller部分下的Is Initial View Controller选项:

在此输入图像描述

You then need to ensure all potential entry points are assigned Storyboard Id 's as you will use this in code from the AppDelegate.swift file. 然后,您需要确保为所有可能的入口点分配Storyboard Id ,因为您将在AppDelegate.swift文件的代码中使用它。 This example uses blue and red 此示例使用bluered

在此输入图像描述

Next you must edit the AppDelegate.swift file. 接下来,您必须编辑AppDelegate.swift文件。 First declare the constant to test against and decide which View Controller to display initially, in this case: 首先声明要测试的常量并决定最初显示哪个View Controller,在这种情况下:

let blueEntryPoint = true

Next insert some code in the default didFinishLaunchingWithOptions function between the comment: // Override point for customization after application launch. 接下来在注释: // Override point for customization after application launch.之间的默认didFinishLaunchingWithOptions函数中插入一些代码, // Override point for customization after application launch. and the statement: return true like so: 和声明:像这样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
}

Run this, then change the blueEntryPoint to false and re-run it! 运行此命令,然后将blueEntryPoint更改为false并重新运行它!

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

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