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

目錄結構與視圖結構:

2.接著我們寫代碼來用來與IB界面編輯器來進行連接:在這里我們可以認為IBOutlet是與IB交互的輸出,而IBAction則是IB交互的事件。
先編寫ViewContoller.h:
~~~
#import <UIKit/UIKit.h>
@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看看效果:
