Uiview not showing up in delegate app

When I execute the code below in the simulator, I expect to see a red fill of the screen, but instead everything is 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; } 

Adding window initialization will not help:

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

My problems started when, after creating a universal window-based project in Xcode, I decided to delete the xib files for iPad / iPhone and the iPhone / iPad delegate files that were automatically created in the project, and instead there is one application delegate with a view controller that creates device-based software presentation. Now I can not show the simple view that I create in the application’s delta.

Edit: I removed the view add and now set the window's background color to red. This does not help, but if I go to the desktop in the simulator and run the application again, now I will have a red screen. Again, I am puzzled by why I do not see red when I first start the application.

+4
source share
2 answers

Below are the steps to configure the universal non-InterfaceBuilder iOS project (.xib).

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

  • Delete all .xib files from the project

  • (Optional) Create a general application delegation class, and then delete the AppDelegate_iPad files. * and AppDelegate_iPhone. *. (Copy any code from existing files before deleting)
  • Modify the main.m file to name the application delegate. I decided to change and use AppDelegate_iPhone for an example. See Documentation: UIApplication Help

    //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; 

    }

  • Update the 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 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; } 
+8
source

I tried to run your code and it worked for me. I got an empty look that was painted red. Have you tried to add breakpoints and verify that your code is actually executing? Otherwise, the only thing I can think of is that it is something with your UIWindow.

+1
source

All Articles