1

As far as i know,if we want to show the UIWindow, we need to do following steps:

  1. create UIWindow
  2. load mian.storyboard and instantiate view controller
  3. set the controller to UIWindow's root viewcontroller, then make UIWindow show

which looks like the following code:

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    //创建窗口对象
    self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
    //创建窗口的根控制器,并且赋值
    UIViewController *rootVc = [[UIViewController alloc]init];
    self.window.rootViewController = rootVc;
    //显示窗口
    [self.window makeKeyAndVisible];
    return YES;
}

or init with storyboard

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


    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];


    UIStoryboard *stroyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];


    UIViewController *vc = [stroyboard instantiateInitialViewController];

    self.window.rootViewController = vc;

    [self.window makeKeyAndVisible];

    return YES;
}

But in the template, i don't see those code, is there something i neglected?

spartawhy117
  • 511
  • 1
  • 5
  • 22

3 Answers3

1

When you choose single view template, some things automatically happens behind the scene.

  • In your IB, the View Controller UI gets the Is Initial View Controller property checked. That means this View Controller is the root view controller.initial view controller

  • Then you see in the above image, your view controller gets an Object ID. This ID is matched with the initialViewController property of your .xml file of the storyboard. If you right-click on your storyboard file and select source code, you will see something like:

    <?xml version="1.0" encoding="UTF-8"?>
    <document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB"
    version="3.0" toolsVersion="12118" systemVersion="16F73"
    targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES"
    useTraitCollections="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
    
    ...
    ...
    
    </document>
    

So basically this is how, the template hooks up for the setup that you previously needed to write from the code. Now you don't have to. These are happening behind the scene of the single view template.

nayem
  • 7,285
  • 1
  • 33
  • 51
0

Thats very old code. Xcode has different flows to load the initial window. You don't need that code anymore, and if you are wondering, you can see how it starts to load by checking the plist. Inside the plist there is a property called "Main Storyboard file base name" aka ""UIMainStoryboardFile", if this one is set then it will load through there.

For reference, This is how you programmatically show your own initial view controller (ignoring the IB one)

Programmatically set the initial view controller using Storyboards

Pochi
  • 13,391
  • 3
  • 64
  • 104
  • so what if i manually write above code to start UIWindow?will it cause some problems? – spartawhy117 Jun 30 '17 at 02:11
  • No, you can choose either method. But if you want to manually set the window you should remove the property from the plist i mentioned. – Pochi Jun 30 '17 at 02:13
  • but i only find key `UIMainStoryboardFile` in plits file ,is this one control the init load? – spartawhy117 Jun 30 '17 at 02:28
  • Yeah, if this key is set then xcode will check the Storyboard file declared there, then it will check "Which View Controller" is marked as root. By removing this property you can safely set the initial view controller as you described in your question. – Pochi Jun 30 '17 at 02:35
0

Execute this flows : Select YourProject->TARGETS->General->Deployment Info->Main Interface , there is a selection textfiled.

If your selected Storyboard.storyboard , then StoryBoard do the initialize automatic. Select Storyboard.storyboard in you project and Open As->Source Code can see this snippet XML code in storyboard :

    <objects>
        <viewController id="BYZ-38-t0r" customClass="ViewController" customModuleProvider="" sceneMemberID="viewController">
            <layoutGuides>
                <viewControllerLayoutGuide type="top" id="y3c-jy-aDJ"/>
                <viewControllerLayoutGuide type="bottom" id="wfy-db-euE"/>
            </layoutGuides>
            <view key="view" contentMode="scaleToFill" id="8bC-Xf-vdC">
                <rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
                <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
                <color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
            </view>
        </viewController>
        <placeholder placeholderIdentifier="IBFirstResponder" id="dkx-z0-nzr" sceneMemberID="firstResponder"/>
    </objects>

Xcode just auto-init with that XML config.

And if your clear the selection textfield , Xcode won't initialize window and ViewController automatic, then you should initialize UIWindow with that code manually.

GrayLand
  • 11
  • 3