Graphics Programs Reference
In-Depth Information
(Why didn't we select UIView as the superclass in the assistant? When you're learning,
it is important to start with the simplest template available in Xcode . Most classes and
projects in this topic will do so. Templates are great for speeding up development, but
they get in the way when you're learning. Typing in each line of code instead of relying
on the “magic” of a template will make you more comfortable when you're writing iOS
applications on your own.)
Let's create an instance of HypnosisView , set its backgroundColor (a property in-
herited from UIView ), and add the HypnosisView to the view hierarchy.
Open HypnosisterAppDelegate.m . At the top of this file, import the header file for
HypnosisView .
#import "HypnosisterAppDelegate.h"
#import "HypnosisView.h"
@implementation HypnosisterAppDelegate
Locate the method application:didFinishLaunchingWithOptions: near
the top of HypnosisterAppDelegate.m . In this method, create an instance of Hyp-
nosisView and add it as a subview of the window.
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
CGRect viewFrame = CGRectMake(160, 240, 100, 150);
HypnosisView *view = [[HypnosisView alloc] initWithFrame:viewFrame];
[view setBackgroundColor:[UIColor redColor]];
[[self window] addSubview:view];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
Build run the application. Notice the red rectangle towards the bottom right corner of the
screen: this is the instance of HypnosisView , which is drawn on top of the UIWindow
(the white background). This HypnosisView instance is a subview of the UIWindow .
When you add a view as a subview of another view, the inverse relationship is automatic-
ally established: the HypnosisView 's superview is the UIWindow . (To avoid a re-
tain cycle, the superview property is declared as __unsafe_unretained .)
Also, notice that the console says something about applications expecting to have a root
view controller. You can ignore this. It doesn't hurt anything, and it will make sense after
the next chapter.
Search WWH ::




Custom Search