先创建一个空白的iphone项目:
选图中已经选中的图标,然后点击next,进入下一步:
为这个项目命名,然后next
选择想要储存的位置,然后创建就ok了
然后创建类来定义界面:
选择图中选中的类进行创建,点击下一步
为类命名,进行下一步
选取路径进行创建
创建好的项目为
在DemoViewController类中定义界面:(类中大多代码为自动生成,在此只写重要代码)
/*DemoViewController.h*/
#import <UIKit/UIKit.h>
@interface DemoViewController : UIViewController
{
//定义一个Label,用来当容器
UILabel * helloLabel;
}
//nonatomic:提高效率,retain:setter方法对参数release旧值,返回新值
@property(nonatomic,retain)UILabel* helloLabel;
@end
/*DemoViewController.m*/
#import "DemoViewController.h"
@implementation DemoViewController
@synthesize helloLabel;//预编译
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
//CGRectMake(CGFloat x, CGFloat y, CGFloat width, CGFloat height)设置Label显示的位置
//CGRectMake(和左边边框的距离,和上边边框的距离,Label的长度,Label的高度)
helloLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 50, 300, 60)];
//设置Label中要显示的内容
helloLabel.text = @"Hello World";
//对齐方式 局中(一行的中间)
helloLabel.textAlignment = UITextAlignmentCenter;
//设置字体颜色为红色
helloLabel.textColor = [UIColor redColor];
//设置字体字号为20
helloLabel.font = [UIFont systemFontOfSize:20];
//把Label添加到View中
[self.view addSubview:helloLabel];
[super viewDidLoad];
}
/*AppDelegate.h*/
#import <UIKit/UIKit.h>
@class DemoViewController;
@interface AppDelegate : UIResponder <UIApplicationDelegate>
{
//创建DemoViewController类的对象
DemoViewController* _iDemoViewController;
}
@property (nonatomic,retain)DemoViewController* iDemoViewController;
@property (strong, nonatomic)UIWindow *window;
@end
/*AppDelegate.m*/
#import "AppDelegate.h"
#import "DemoViewController.h"
@implementation AppDelegate
//备份变量名 通俗理解:作用就是让编写者学会运用self
@synthesize window = _window;
@synthesize iDemoViewController = _iDemoViewController;
- (void)dealloc
{
self.iDemoViewController = nil;
[_window release];
[super dealloc];
}
- (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];
//创建出一个临时的Demo
DemoViewController *demo = [[DemoViewController alloc]init];
self.iDemoViewController = demo;
[demo release];//释放临时Demo
//添加到window中
[self.window addSubview:self.iDemoViewController.view];
[self.window makeKeyAndVisible];
return YES;
}
然后运行项目就可以得到我们特别熟悉而又陌生的HelloWorld了