Iphone開發入門之基本交互

 


      首先我們需要了解MVC模式(Model-View-Controller)。也許有些人和我一樣,以前學過WEB開發。

所以對MVC比較瞭解。但是真正運用開發時只知道其中的實現。對於具體的理論我就不做過多的介紹。

效果如圖所示:

當我們點擊onClick按鈕時會改變上面Label的內容。
代碼如下:

 首先在TestAlertViewController 中聲明兩個變量。也就是button和label;

然後在聲明一個方法,是點擊按鈕時觸發事件所要處理的方法

#import "MyAlertView.h"
#import <UIKit/UIKit.h>

@interface TestAlertViewController : UIViewController {
	UIButton *myButton;
	UILabel *myLabel;
}
@property (nonatomic, retain) UIButton *myButton;
@property (nonatomic, retain) UILabel *myLabel;
-(IBAction) buttonPress:(id)sender;
@end

在TestAlertViewController 實例化兩個變量。並完成處理事件的方法。

-(IBAction) buttonPress:(id)sender {
     myLabel.text = @"onClick myButton!";
}
 
- (void)viewDidLoad {
	myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
	myButton.frame = CGRectMake(120, 220, 80, 40);
	[myButton setBackgroundColor:[UIColor blueColor]];
	[myButton setTitle:@"onClick" forState:UIControlStateNormal];
	[myButton addTarget:self action:@selector(buttonPress:) forControlEvents:UIControlEventTouchUpInside];
//添加myButton處理事件的方法。
	[self.view addSubview:myButton];
	myLabel = [[UILabel alloc] initWithFrame:CGRectMake(90, 50, 140, 40)];
	myLabel.textAlignment = UITextAlignmentCenter;
	myLabel.text = @"HelloWorld";
	[self.view addSubview:myLabel];
    [super viewDidLoad];
}



- (void)viewDidLoad {
	myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
	myButton.frame = CGRectMake(120, 220, 80, 40);
	[myButton setBackgroundColor:[UIColor blueColor]];
	[myButton setTitle:@"onClick" forState:UIControlStateNormal];
	[myButton addTarget:self action:@selector(buttonPress:) forControlEvents:UIControlEventTouchUpInside];
	[self.view addSubview:myButton];
	myLabel = [[UILabel alloc] initWithFrame:CGRectMake(90, 50, 140, 40)];
	myLabel.textAlignment = UITextAlignmentCenter;
	myLabel.text = @"HelloWorld";
	[self.view addSubview:myLabel];
    [super viewDidLoad];
}


 

 

 

 


- (void)viewDidLoad {
	myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
	myButton.frame = CGRectMake(120, 220, 80, 40);
	[myButton setBackgroundColor:[UIColor blueColor]];
	[myButton setTitle:@"onClick" forState:UIControlStateNormal];
	[myButton addTarget:self action:@selector(buttonPress:) forControlEvents:UIControlEventTouchUpInside];
	[self.view addSubview:myButton];
	myLabel = [[UILabel alloc] initWithFrame:CGRectMake(90, 50, 140, 40)];
	myLabel.textAlignment = UITextAlignmentCenter;
	myLabel.text = @"HelloWorld";
	[self.view addSubview:myLabel];
    [super viewDidLoad];
}


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章