程序要求
實現點擊一個按鈕,在對應的文本框內顯示當前日期時間(Xcode6.1版本)
實現主要步驟
新建一個IOS的基本工程修改界面,打開Main.storyboard,使用界面編輯器(IB)拖拽出一個按鈕和文本編輯框 (View)在control裡面新建按鈕事件響應函數,-(IBAction)onClickButton:(id)sender (Control)在control裡面新建文本框的連接變量,使得修改該變量反映在修改文本框上 (Control)在界面編輯器裡(IB)面,按住ctrl,從按鈕出拉出一條直線到上面的View Control上,然後選擇列表中的按鈕事件響應函數即可實現按鈕按下和響應函數之間的連接。(或者點擊按鈕,從右側的工具欄Show the connections inspector列表中的Touch Up Inside拉一條直線到窗口上面的View Control,然後選擇事件響應函數即可) (從界面拉直線到View
Control)打開界面編輯器(IB),從View Control拉一條直線到文板框,然後選擇列表中定義的文板框變量即可實現文板框和變量的連接(從View Control拉直線到界面)實現邏輯代碼,包括設置變量,響應函數的編寫
示例代碼
ViewController.h
// // ViewController.h // 1129_HelloWolrd // // Created by God Lin on 14/11/29. // Copyright (c) 2014年 arbboter. All rights reserved. // #import @interface ViewController : UIViewController { UITextField* textTime; } @property (nonatomic, retain) IBOutlet UITextField* textTime; -(IBAction)onClickTimeBtn:(id)sender; @end
ViewController.m
// // ViewController.m // 1129_HelloWolrd // // Created by God Lin on 14/11/29. // Copyright (c) 2014年 arbboter. All rights reserved. // #import "ViewController.h" @interface ViewController () @end @implementation ViewController @synthesize textTime; -(IBAction)onClickTimeBtn:(id)sender { if(textTime) { NSDate* date = [NSDate date]; textTime.text = [date description]; } } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end