新建一个iPhone的Window BaseView工程
然后在Class中新建文件UIViewControllerSubclass文件,我们使用Hello文件名,勾选下面的With XIB for user Interface,选择next
OK,文件新建成功,在文件列表中出现Hello.h,Hello.m,Hello.xib,可以把xib移动到Resource文件夹中,这样规范一点
然后我们打开Hello.h
我们自定义一个方法为SayHello,方法体如下:
- (IBAction) SayHello;
然后选择相对应的Hello.m文件,也可以按快捷键Command+Ctrl+向上的光标键
我们来实现这个方法,代码如下
- (IBAction)SayHello
{
UIAlertView *view=[ [UIAlertView alloc]initWithTitle:@"提示" message:@"You Got it." delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil]; //初始化UIAlertView,并写入相关参数,学过C#的可以理解为MessageBox msgBox=new MessageBox(Title,Content....)
[view show]; //这边就是把刚才的view显示出来 msgBox.show();
[view release]; //这个是释放资源
}
OK,SayHello方法完成
然后我们打开系统的degelate.h文件,来声明我们刚才写的类
首先需要import "Hello.h" //这个很类似java的写法
然后我们在接口初始化方法里初始化我们刚才写的类
Hello *hello ;
在方法外来引用
@property (nonatomic,retain) Hello *hello;
完整代码如下:
//
// RepeatHelloAppDelegate.h
// RepeatHello
//
// Created by Scott on 10-10-18.
// Copyright 2010 __MyCompanyName__. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "Hello.h"
@interface RepeatHelloAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
Hello *hello;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic,retain) Hello *hello;
@end
下面我们来搞定相对应的.m实现文件
打开文件我们首先看到@synthesize这样的关键字,还有上面的@property,我们参见http://www.360doc.com/content/10/0329/09/1014216_20687179.shtml
我们先@synthesize hello;紧接着在didFinishLaunchingWithOptions方法里写初始化
hello=[[Hello alloc]init]; addSubview];
[window:hello.view];
OK,整个编码部分已经完成,下面就开始拖拉了
打开Hello.xib,然后在界面上拖上一个RoundRectButton
右击Button,选择touch inside事件,连线到File's Owner,选择SayHello方法,OK,运行,搞定!