创建iOS应用HelloWorld

  • 第一个Swift程序

在应用文件夹打开 Xcode开发工具,打开后在屏幕顶部选择 File => New => Playground

接着 为 playground 设置一个名字并选择 iOS 平台

Swift 的 playground 就像是一个可交互的文档,它是用来练手学swift的,写一句代码出一行结果(右侧),可以实时查看代码结果,是学习swift语言的利器

 

  • 第一个Swift项目

  • 打开 xcode 工具,选择 File => New => Project

 

  1. 我们选择一个"Single View Application",并点击"next",创建一个简单示例app应用

 

  1. 接着我们输入项目名称(ProductName),公司名称(Organization Name),公司标识前缀名(Organization identifier) 还要选择开发语言(Language),选择设备(Devices)。

 

其中Language有两个选项:Objective-c和swift,因为我们是学习swift当然选择swift项了。 点击"Next"下一步。

 

  1. 选择存放的目录,如果要使用Git源代码管理,将勾上Source Control的create git repository on My Mac. 点击create创建项目

 

  1. 项目创建后,默认生成了一个示例文件,可以看到swift将oc中的h和m文件合并成了一个文件(即swift后缀名文件). Main.storyboard相当于xib文件,有比xib更多的功能

  1. 打开main.storyboard,默认看到一个简单的空白的应用界面,大小为平板界面大小。 如果开发都只需要开发兼容iphone手机的app,那么可以把Use Auto Layout的勾去掉(默认为勾上)

 

  1. 弹出了一个对话框,让我们选择界面尺寸,iPhone 或 iPad。我们选择iPhone的尺寸

8、可以看到,界面大小变为了手机iphone的宽度和高度。

大家可以记住界面相关的尺寸,方便以后布局计算位置

  1. 我们为界面添加点内容,在右下方找到Text控件,将它拖入storyboard上,并双击写入文本"Hello World!"。

运行一下模拟器(command+R 快捷键或在菜单栏中选择 Product => Run)。

至此,我们的第一个Swift项目就完成了

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
// // main.m // Hello World_Code // #import #import "AppDelegate.h" int main(int argc, char *argv[]) { @autoreleasepool { return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); } } // // AppDelegate.h // Hello World_Code // #import @interface AppDelegate : UIResponder @property (strong, nonatomic) UIWindow *window; @end // // AppDelegate.m // Hello World_Code // #import "AppDelegate.h" @implementation AppDelegate - (void)dealloc { [_window release]; [super dealloc]; } - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyAndVisible]; UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(320/2.0 - 140/2.0, 80, 140, 40)]; // 1 label.text = @"Hello World"; label.backgroundColor = [UIColor cyanColor]; label.textAlignment = NSTextAlignmentCenter; [self.window addSubview:label]; // 2 [label release]; // 1 return YES; } - (void)applicationWillResignActive:(UIApplication *)application { // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. } - (void)applicationDidEnterBackground:(UIApplication *)application { // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值