读书人

iOS hello world 解决官方教程不能运行

发布时间: 2012-08-28 12:37:01 作者: rapoo

iOS hello world 解决官方教程不能运行问题
经过两天的折腾,终于将iOS开发环境搭建起来。公司用的是Mac mini server 进行开发不光要搭建软件环境,还要搭建硬件环境十分复杂,而且公司的网速非常慢,下载xcode和系统更新尤其是个大问题。今天整整跑了4趟网吧才搞定。
言归正传,在安装官方的例子
http://developer.apple.com/library/ios/#documentation/Xcode/Conceptual/iphone_development/100-iOS_Development_Quick_Start/development_quick_start.html#//apple_ref/doc/uid/TP40007959-CH3-SW1
写hello world的时候遇到一个问题就是程序一闪而过,没有报错,编译成功,对与新手来说往往不知所措。
对比官方提供的源码发现。MyView.h 内需要做更改,即MyView.h需要继承UIView.
更改后的MyView.h代码如下:

// MyView.h#import <UIKit/UIKit.h> @interface MyView : UIView {} @end

另外在MyView.m中需要添加一个initWithFrame方法。

- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
// Initialization code
}
return self;
}

也就是说MyView.m修改后如下:
// MyView.m#import "MyView.h" @implementation MyView - (id)initWithFrame:(CGRect)frame {    if (self = [super initWithFrame:frame]) {        // Initialization code    }    return self;} - (void)drawRect:(CGRect)rect {   NSString *hello   = @"Hello, World!";   CGPoint  location = CGPointMake(10, 20);   UIFont   *font    = [UIFont systemFontOfSize:24];   [[UIColor whiteColor] set];   [hello drawAtPoint:location withFont:font];} - (void)dealloc {    [super dealloc];} @end


这些东西在前面的教程中都没有提到,我发现很多hello world程序都是存在一些小bug。不知道是作者坑爹还是故意为之。

读书人网 >操作系统

热点推荐