Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

uiview doesn't show in app delegate

When I execute the code below in the simulator I expect to see red fill the screen, but instead it's all black, why?

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  UIView * myView = [[UIView alloc] initWithFrame:[window bounds]];
  [myView setBackgroundColor:[UIColor redColor]];
  [window addSubview:myView];
  [window makeKeyAndVisible];
  return YES;
}

If I add initialization of window, that doesn't help:

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

My troubles begun when after creating a Window-based Universal project in Xcode I decided to delete the iPad/iPhone xib files and the iPhone/iPad app delegate files that were automatically created in the project, and instead have a single app delegate with a view controller that builds the view programmatically based on the device. Now I can't seem to display a simple view I create in the app delegate.

Edit: I removed adding a view, and now set the background color of the window to red. That doesn't help, but if I go to desktop in the simulator and reopen the running app I get now a red screen. Again, I'm puzzled as to why I don't see red the first time I launch the app.

like image 388
andrewz Avatar asked Jan 21 '23 22:01

andrewz


1 Answers

Here are the steps to setup a non-InterfaceBuilder (.xib) Universal iOS project.

  1. Delete all associations to the .xib files in your property list for the application. Delete the .xib file references from your application plist

  2. Delete all the .xib files from the project

  3. (Optional) Create a common Application Delegate class, and then delete the AppDelegate_iPad.* and the AppDelegate_iPhone.* files. (Copy paste any code from the existing files before deleting)
  4. Change your main.m file to name the application delegate. I chose to modify and use the AppDelegate_iPhone for example purposes. See the documentation: UIApplication Reference

    // main.m int main(int argc, char *argv[]) {

    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    // Start an iPhone/iPad App using a .xib file (Defaults set in .plist)
    //int retVal = UIApplicationMain(argc, argv, nil, nil);
    
    // Start the iPhone/iPad App programmatically
    // Set the 3rd argument to nil, to use the default UIApplication
    // Set the 4th argument to the string name of your AppDelegate class
    int retVal = UIApplicationMain(argc, argv, nil, @"AppDelegate_iPhone");
    
    [pool release];
    return retVal;
    

    }

  5. Update your application delegate code.

    // AppDelegate_iPhone.m - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

        // Override point for customization after application launch.
        NSLog(@"Launch my application iphone");
    
        // Create the window, it's not created without a nib
        window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
    
        // Create a subview that is red
        UIView *myView = [[UIView alloc] initWithFrame:[window bounds]];
        [myView setBackgroundColor:[UIColor redColor]];
    
        // Add the subview and release the memory, sicne the window owns it now
        [self.window addSubview:myView];
        [myView release]; 
    
        [self.window makeKeyAndVisible];
    
        return YES;
    }
    
like image 74
Paul Solt Avatar answered Feb 04 '23 04:02

Paul Solt