ios學習筆記(二)xcode 4.3.2下實現基本交互
想必大家都閱讀過iphone4與ipad2開發基礎教程吧,這本書的xcode與現在的最新版本有些區別,去掉了view base application,只有比較接近的single view application.
首先我們創建一個single view application,注意這里我們不用自動引用計數。
接著我們點擊工程列表中的MainStoryboard_iphone.storyboard 來編輯界面:
我們創建兩個按鈕和一個用來輸出的空白文本:

目錄結構與視圖結構:

2.接著我們寫代碼來用來與IB界面編輯器來進行連接:在這里我們可以認為IBOutlet是與IB交互的輸出,而IBAction則是IB交互的事件。
先編寫ViewContoller.h:
#import@interface ViewController : UIViewController { } @property (nonatomic,retain) IBOutlet UILabel * statusText; -(IBAction)buttonPressed:(id)sender; @end
接著在ViewContoller.m來實現功能:
#import "ViewController.h"
@implementation ViewController
@synthesize statusText;
-(IBAction)buttonPressed:(id)sender
{
NSString *title = [sender titleForState:UIControlStateNormal];//sender用于獲得了不同的button的文字 titleForState是根據button狀態獲取文字的函數
NSString *newText = [[NSString alloc] initWithFormat:@"%@ button pressed.", title];//將title中的文字放入newText中
statusText.text = newText;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
self.statusText = nil;
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
} else {
return YES;
}
}
- (void)dealloc
{
[statusText release];
[super dealloc];
}
@end接著我們把這些代碼與IB連接起來:
選擇MainStoryboard_iphone.storyboard 右擊View Controller Scene中的 Buton,選擇Touch down與界面的
bottonPressed相連接:

接著將lable和statusText連接在一塊:

最后運行程序,點擊botton看看效果:

:http://blog.csdn.net/itachi85/article/details/7628935
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!