starting ios project without storyboard

前端 未结 9 1229
渐次进展
渐次进展 2021-01-12 23:28

Im having some troubles to start an iOS app using xibs instead of storyboard. The problem is that im getting a black screen and the first view controller is not being called

相关标签:
9条回答
  • 2021-01-12 23:38
    1. Leave main interface empty

    2. Add new ViewController.xib to the project and mark its file's owner class as "ViewController"

    3. In AppDelegate.m

      - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
      self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
      
      NSArray* xibContents = [[NSBundle mainBundle] loadNibNamed:@"ViewController"
                                                           owner:nil
                                                         options:nil];
      ViewController* vc = [xibContents objectAtIndex:0];
      UINavigationController *navigationController =
          [[UINavigationController alloc] initWithRootViewController:vc];
      self.window.rootViewController = navigationController;
      [self.window makeKeyAndVisible];
      
      return YES;
      

      }

    4. build and run

    0 讨论(0)
  • 2021-01-12 23:43

    Xcode 11 with SceneDelegate

    In SceneDelegate.m

    - (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions {
    // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
    // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
    // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.windowScene = (UIWindowScene *)scene;
    self.viewController = [[ViewController alloc] init];
    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
    self.window.rootViewController = navController;
    [self.window makeKeyAndVisible];
    }
    

    No need to do anything in AppDelegate.m

    0 讨论(0)
  • 2021-01-12 23:50

    To change your project to use xibs instead of storyboards start by creating a xib for each view controller. You will need to change the File's Owner to class to the view controller you are creating the xib for. Then link the File's Owner view outlet to the view in the xib.

    After that, select your app target and change the Main Interface drop down to be empty. Now you can delete the storyboard file.

    Finally, initialize your window in the app delegate's application:didFinishLaunchingWithOptions: method and set your initial view controller as the root view controller of the window. Then call makeKeyAndVisible on your app delegate's window and you should be good to go.

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        // Override point for customization after application launch.
        self.window = [UIWindow new];
        self.window.rootViewController = [ViewController new];
        [self.window makeKeyAndVisible];
        return YES;
    }
    
    0 讨论(0)
  • 2021-01-12 23:56

    Hope this helps you:

    Delete the view controller and storyboard file and new viewController.h,viewController.h.m ,viewController.xib file.

     #import "AppDelegate.h"
    
     @interface AppDelegate ()
    
     @end
    
     @implementation AppDelegate
     @synthesize viewCOntrollerobj;
    
     - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
         self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
         self.viewCOntrollerobj = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
         UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:self.viewCOntrollerobj];
         //navController.navigationBarHidden = YES;
    
         self.window.rootViewController = navController;
         [self.window makeKeyAndVisible];
         return YES;
    
    }  
    

    0 讨论(0)
  • 2021-01-12 23:57

    I didnt try with XIB, but this thing is working fine here:::

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        self.viewController = [[ViewController alloc] init];
        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
        navController.navigationBarHidden = YES;
    
        self.window.rootViewController = navController;
        [self.window makeKeyAndVisible];
        // Override point for customization after application launch.
        return YES;
    }
    

    and in ViewController's viewDidLoad

    - (void)viewDidLoad {
        [super viewDidLoad];
        [self.view setBackgroundColor:[UIColor greenColor]];
        // Do any additional setup after loading the view, typically from a nib.
    }
    

    And the green color comes on the screen.

    Initialize the window before setting the root view controller for the window.

    That's the only problem that i see in you code, as the rootViewController is set to a nil window, and after that you are initializing.

    0 讨论(0)
  • 2021-01-12 23:58

    OK, at last i got it.

    What i had to do is just add again

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

    After this, just delete the .h, .m and .xib and create them again.

    For any reason its working fine now.

    0 讨论(0)
提交回复
热议问题