IOS开发从hello world开始
学习软件开发少不了讲解hello world,ios hello world小程序对大家肯定没有什么问题了,在这里我用纯代码的方式建立一个Hello world工程,以供ios开发初学者参考了,
首先打开Xcode新建一个empty Application模版,之所以建成空模板是为了从头开始建立一个类似于SingleView Application的模版,然大家熟悉一下建立一个单个窗口程序的过程
建好后项目如下图
接下来需要创建一个基于UIViewController的视图控制器类,取名HelloWroldViewController,
HelloWroldViewController控制着程序的视图,在该类的头文件中添加如下代码以新建一个button,
#import <UIKit/UIKit.h> @interface HelloWroldViewController : UIViewController { UIButton * helloworldButton; } @property(nonatomic,retain)UIButton * helloworldButton; @end
HelloWroldViewController.m中添加如下代码
@synthesize helloworldButton = _helloworldButton; - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. //设置按钮的外观类型 self.helloworldButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; //设置按钮的位置 self.helloworldButton.frame = CGRectMake(120, 300, 100, 40); //设置按钮标题 [self.helloworldButton setTitle:@"hello world" forState:UIControlStateNormal]; [self.helloworldButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal]; [self.view addSubview:self.helloworldButton]; [self.helloworldButton release]; }
接下来转到
AppDelegate.h添加如下代码,
@class HelloWroldViewController; @interface AppDelegate : UIResponder <UIApplicationDelegate> @property (strong, nonatomic) UIWindow *window; @property(nonatomic,retain)HelloWroldViewController * rootViewController; @end
AppDelegate.m文件里引入
#import "HelloWroldViewController.h" @implementation AppDelegate @synthesize rootViewController = _rootViewController; - (void)dealloc { [_window release]; [_rootViewController 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];
self.rootViewController = [[HelloWroldViewController alloc]initWithNibName:nil bundle:nil];
self.window.rootViewController = self.rootViewController; [self.window makeKeyAndVisible]; return YES; }
最后项目运行图