自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Swift學(xué)習(xí)之UI開發(fā)初探

移動開發(fā) iOS
Swift是供iOS和OS X應(yīng)用編程的新編程語言。相信很多開發(fā)者都在學(xué)習(xí)這門新語言。廢話不多說,下面我就來學(xué)習(xí)使用Swift創(chuàng)建一個簡單的UI應(yīng)用程序。

概述

Apple近日發(fā)布了Swift編程語言,Swift是供iOS和OS X應(yīng)用編程的新編程語言。相信很多開發(fā)者都在學(xué)習(xí)這門新語言。

廢話不多說,下面我就來學(xué)習(xí)使用Swift創(chuàng)建一個簡單的UI應(yīng)用程序。

關(guān)于Swift語法,可以參考《蘋果Swift編程語言快速上手入門教程》&《蘋果的新編程語言 Swift 簡介

效果如下:

開發(fā)環(huán)境

Xcode6-beta 

iOS 8

創(chuàng)建工程

1. Choose File > New > Project > (iOS or OS X) > Application > your template of choice.

此處選擇 Single view Application。

2. Click the Language pop-up menu and choose Swift. 

添加基本控件

在ViewController.swift文件中進行編碼,該文件類似Objective-C的ViewController.m。

UILabel

UILabel 控件常用于顯示文本標(biāo)簽。

下面我們來創(chuàng)建一個label, 查看UILabel類發(fā)現(xiàn)其繼承于UIView, NSCoding。

可以通過類似創(chuàng)建view的方法,設(shè)置大小和lebel的text,通過addSubview方法將其加到當(dāng)前view上。

代碼如下:

  1. let label = UILabel(frame:CGRect(origin: CGPointMake(10.0, 50.0), size: CGSizeMake(150,50)))//let 是Swift 表示常量的關(guān)鍵字  
  2. label.text = "This is a Label"  
  3. self.view.addSubview(label)  

UILabel創(chuàng)建參數(shù)使用了別名,這點像Object-C。

UIButton

UIButton 控件常用于按鈕。

下面我們來創(chuàng)建一個button按鈕,并設(shè)置它的UIControlEvents.TouchUpInside事件的處理,查看UIButton類發(fā)現(xiàn)其繼承于UIControl, NSCoding。

可以通過類似創(chuàng)建view的方法,指定位置和大小創(chuàng)建一個按鈕,然后設(shè)置按鈕的titile,設(shè)置按鈕的背景色,并設(shè)置按鈕的touch事件。

最后通過addSubview方法將其加到當(dāng)前view上。

代碼如下:

  1. let btn = UIButton(frame:CGRect(origin: CGPointMake(10.0, 110.0), size: CGSizeMake(150,50)))  
  2. btn.setTitle("button", forState: UIControlState.Normal)  
  3. btn.backgroundColor = UIColor.redColor()  
  4. btn.addTarget(self, action: "buttonClick:", forControlEvents: UIControlEvents.TouchUpInside)  
  5. self.view.addSubview(btn)  

buttonClick方法實現(xiàn)如下:

  1. func buttonClick(sender: UIButton!){  
  2.    
  3.     }  

UIButton后面的 ”!“ 意味著,sender可以是由UIButton繼承來的任意子類。

UIAlertView

UIAlertView 常用于彈出對話框,下面我們來創(chuàng)建一個alert。

UIAlertView類繼承于UIView,我們先創(chuàng)建了一個alert,然后設(shè)置alert的title、message、button、delegate。

然后調(diào)用UIAlertView的show方法,顯示alert。

我們是在button的touch回調(diào)事件中處理alert的創(chuàng)建和顯示的。在buttonClick方法中添加如下代碼:

  1. var alert = UIAlertView()  
  2. //直接這樣創(chuàng)建有bug  
  3. //var alert = UIAlertView(title: "alert", message: "this is an alert", delegate: self, cancelButtonTitle: "cancel")  
  4. alert.title = "alert"  
  5. alert.delegate = self  
  6. alert.addButtonWithTitle("cancel")  
  7. alert.message = "this is an alert"  
  8. alert.show()  

delegate和self,依然有Object-C的影子。

修改ViewController的聲明,加入UIAlertViewDelegate

  1. class ViewController: UIViewController, UIAlertViewDelegate  

實現(xiàn)alert的delegate方法,處理button的click事件。

  1.  //處理alert 的button click  
  2. func alertView(alertView: UIAlertView!, clickedButtonAtIndex buttonIndex: Int){  
  3. println("buttonIndex:\(buttonIndex)")  
  4.     }  

總結(jié)

Swift 的UIKit API接口和 Objective-C的API接口總體上保持一致,熟悉原來的UIKit接口的話,上手Swift UI開發(fā)應(yīng)該很快。

可以通過文檔和API手冊查看各Objective-C的API 如何使用Swift 的API進行編程。

點擊這里獲取本文的Demo。

整理自泰然網(wǎng)(作者:ZeroYang)

 

責(zé)任編輯:閆佳明 來源: tairan
相關(guān)推薦

2015-04-17 16:07:11

swiftOC

2021-10-18 10:14:26

鴻蒙HarmonyOS應(yīng)用

2013-07-24 18:14:36

Android開發(fā)學(xué)習(xí)Android UIButton

2013-07-24 18:02:40

Android開發(fā)學(xué)習(xí)Android UIRadio、Check

2011-06-16 10:25:29

AndroidAIR

2015-06-23 15:48:41

Swift 2.0iOS9

2010-09-25 13:09:39

UISymbian

2014-09-26 09:49:48

SwiftObjective-C

2017-01-15 18:32:39

Openresty架構(gòu)性能

2011-08-01 18:27:58

iPhone開發(fā) UISearchBa

2022-04-27 08:37:54

系統(tǒng)Linux

2011-04-14 10:05:16

BlackBerry

2011-04-14 10:03:32

UI組件BlackBerry

2014-07-21 09:43:57

2010-10-09 15:01:27

PhoneGapiPhoneAndroid

2012-03-16 13:43:29

2013-09-16 15:50:04

Android優(yōu)化界面UI

2013-09-16 15:33:28

Android優(yōu)化界面UI

2011-09-08 10:41:12

Node.js

2013-11-20 14:56:40

iOS 7.1優(yōu)化
點贊
收藏

51CTO技術(shù)棧公眾號