1.第一种,代码方式:
添加在下面这个方法体内:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
//开始添加视图
CGRect viewRect = CGRectMake(0, 0, 460, 960);
UIView *rootView = [[UIView alloc]initWithFrame:viewRect];
[rootView setBackgroundColor:[UIColor blueColor]];
[self.window addSubview:rootView];
UILabel *hellLable = [[UILabel alloc]initWithFrame:CGRectMake(160-50, 30, 100, 50)];
hellLable.text = (@"hello");
[hellLable setTextAlignment:NSTextAlignmentCenter];
[rootView addSubview:hellLable];
[rootView release];
//添加视图完毕
return YES;
}
2.第二种方法:通过创建nib文件,然后添加nib文件到程序中。
a:开始创建一个nib文件:new file -- IOS -- User Interface - View ;
b:在AppDelegate.h文件中,与方法一相同的位置添加以下代码:
NSArray *view = [[NSBundle mainBundle] loadNibNamed:(@"View") owner:self options:nil];
[self.window addSubview:[view lastObject]];
添加结束。