IOS成长之路-第一个Hello World项目

本文详细介绍了如何使用Objective-C语言在iOS平台上创建一个简单应用,并通过编写代码实现显示HelloWorld的界面。从项目创建、界面设计到运行应用,涵盖了从零开始构建iOS应用的基本步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

先创建一个空白的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了

 

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值