從零開始學ios開發(四):IOS控制項(1),Image View、Text Field、Keyboard

來源:互聯網
上載者:User

長話短說,謝謝大家的關注,這篇寫了好長時間,下面繼續學習ios。我將用2到3篇的篇幅來學習iphone上的一些常用控制項,包括Image View、Text Field、Keyboard、Slider等等,這篇的內容包括ImageView和Keyboard的使用。完成後的如下:

1)建立一個新的project,選擇“Single View Application”,命名為“Control Fun”,然後儲存。
2)添加ImageView
3)上傳圖片到project中,整個過程很簡單,選中某一張圖片,拖到project navigator中的Supporting Files檔案夾中
4)為ImageView添加圖片
5)調整圖片大小
對於圖片大小和顯示方式的處理,iphone建議的是提供適當大小的圖片(即圖片的大小和最終要顯示在iphone上的大小是一致的,不需要iphone的cpu進行更多的處理,這樣有助於程式的快速運行和節約cpu的消耗),如果同一張圖片有2種大小需要顯示,那麼你就傳2張圖片到程式了就可以了。

6)調整ImageView到圖片的原始大小
7)添加2個Label和2個textfield
8)重新命名Label,調整textfield大小
9)為textfield添加Placeholder文字
至此,所有的介面布局都已經完成,下面進入code階段,將學習如何調用iphone的虛擬鍵盤。

10)虛擬數字鍵台
此時,一個新的問題又出現了,當我們輸入結束後,如何隱藏鍵盤?當點擊虛擬鍵盤的“return”按鍵時,它會調用一個Event,叫“Did End On Exit event”,我們可以關聯一個Action到這個event,當觸發這個event時,調用Action使鍵盤隱藏,下面說說具體的隱藏鍵盤的方法。

11)建立Outlet
#import <UIKit/UIKit.h>@interface BIDViewController : UIViewController@property (weak, nonatomic) IBOutlet UITextField *nameField;@property (weak, nonatomic) IBOutlet UITextField *numberField;@endBIDViewController.m

#import "BIDViewController.h"@implementation BIDViewController@synthesize nameField;@synthesize numberField;......我們添加了2個指向textfield的Outlet,分別命名為nameField和numberField。

12)手動添加一個Action
#import <UIKit/UIKit.h>@interface BIDViewController : UIViewController@property (weak, nonatomic) IBOutlet UITextField *nameField;@property (weak, nonatomic) IBOutlet UITextField *numberField;- (IBAction)textFieldDoneEditing:(id)sender;@endb)在BIDViewController.m的最後實現textFieldDoneEditing方法

- (IBAction)textFieldDoneEditing:(id)sender{    [sender resignFirstResponder];}

(下面是我個人對resignFirstResponder的理解,如果有錯,希望大家能夠指正,謝謝!)
13)關聯Action
14)實現原理
選中Objects中的根View,在identity inspector中,將UIView改成UIControl。
15)建立另一個Action:backgroundTap
- (IBAction)backgroundTap:(id)sender;

BIDViewController.m

- (IBAction)backgroundTap:(id)sender{    [nameField resignFirstResponder];    [numberField resignFirstResponder];}

16)關聯Action
再次編譯運行程式,在第二個textfield中輸入數字,點擊螢幕上的任何地方,虛擬數字鍵台消失
至此,所有的功能開發完畢。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.