1

I'm setting First viewController which will appear on App Startup. That's my AppDelegate.h:

#import "AppDelegate.h"
#import "TutorialController.h" // a simple UIViewController

@implementation AppDelegate

@synthesize window;

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

    window.rootViewController = [[TutorialController alloc] init];

    [window makeKeyAndVisible];

    return YES;
}

It doesn't give any alert but, launching app, after splashscreen, it appear only a black screen. Without that code everything works fine. I can't do that in StoryBoard because, after solving this trouble, I've got to add other things... What could be wrong? Thank you!

SOLVED: Solved using followben's reply.

Community
  • 1
  • 1
Matte.Car
  • 2,007
  • 4
  • 23
  • 41
  • Create a window with `window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];` before setting the rootViewController? – SolidSun Oct 21 '13 at 16:54
  • No, I've just initialized `@property (strong, nonatomic) UIWindow *window;` in .h but, adding your string, problem persists... – Matte.Car Oct 21 '13 at 17:00
  • a property can't be initialised in a header. a header can only declare stuff. Disclaimer: I know there are exceptions but this is the simplified truth – Daij-Djan Oct 22 '13 at 09:30

3 Answers3

8

.h:

@class MainViewController;

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;
@property (strong,nonatomic) MainViewController *mainViewController;

.m:

#import "MainViewController.h"

@implementation AppDelegate
@synthesize mainViewController;


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


    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.mainViewController = [[MainViewController alloc] initWithNibName:@"MainViewController" bundle:nil];
    self.window.rootViewController = self.mainViewController;
    [self.window makeKeyAndVisible];
    return YES;
}

EDIT, with the complete project link (zipped) ( working :) )

EDIT2:

if you are working with Storyboard, make a storyboard file, and assign it with the project's storyboard like you see on this picture: Main interface is the same as your storyboard file name.

enter image description here and you don't have to write anything into your app delegate:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    return YES;
}

You can set the starting page by moving this little arrow to a ViewController which you want to set the starter like on this pic:

enter image description here

if you want to assign your ViewController ( in storyboard ) with your new TutorialViewController make it like this:

enter image description here

Magyar Miklós
  • 4,182
  • 2
  • 24
  • 42
  • 'Could not load NIB in bundle: 'NSBundle – Matte.Car Oct 21 '13 at 17:55
  • so you don't have a XIB = NIB named "MainViewController", then use self.mainViewController = [[MainViewController alloc] init]; – Magyar Miklós Oct 21 '13 at 17:58
  • Sorry but probably I'm doing a very big mistake, could you tell me where I'm wrong? Sorry but I'm going crazy!! Thank you so much!! https://copy.com/lZ6LRKBhbuQn4rzN – Matte.Car Oct 21 '13 at 21:04
  • I ve downloaded your project, and solved your problem, I edited my answer where you can find the link with the working project :) – Magyar Miklós Oct 22 '13 at 09:18
  • Thank very much! But how can I solve if I want to load a View made in Storyboard and not by code? – Matte.Car Oct 22 '13 at 10:49
  • Thank you but I need to do it by AppDelegate as I said in question...I found this stackoverflow.com/questions/8451975/… and I'm trying to applying it to my project but I can't find a valid solution... – Matte.Car Oct 22 '13 at 16:55
1

Till what I know, this issue happens due to -(void)loadView method. So remove or comment this method and instead use - (void)viewDidLoad to load everything.

Cian
  • 263
  • 4
  • 8
0

For me it is because probably i run iOS 12.4 but in the default iOS projet is some Scene Delegate which is annoted to be available only for iOS 13. Good job apple for confusing developers by making not working code.

luky
  • 2,263
  • 3
  • 22
  • 40