IOS-入門樣本
程式要求
實現點擊一個按鈕,在對應的文字框內顯示當前日期時間(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