Just created a new project in Xcode (View based), and tried creating a button like this programmatically, but it doesn’t show. Why not?
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// Override point for customization after app launch
[window addSubview:viewController.view];
CGRect rect = CGRectMake(0, 20,100,20);
UIButton *button = [[UIButton alloc] initWithFrame:rect];
[viewController.view addSubview:button];
[window makeKeyAndVisible];
}
Better that you don’t use the application delegate to set up your view, just load your first root view. So change the appDelegate’s applicationDidFinishLaunching: back to
And go into your view that it calls and punch in this code, that’ll create your UIButton for you. I’ve also added the change of the background color to make sure you’re loading this view.
Usually I’d prefer to programatically build my views since I can override drawRect: which you can’t do inside InterfaceBuilder afaik.