CGRect frame = CGRectMake(10.0f, 10.0f, 120.0f, 50.0f); android
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];//初始化一個UIButton ios
button.frame = frame;//設置button的大小和位置 ide
[button setTitle:@"title"forState:UIControlStateNormal];//設置button正常狀態下的文字 學習
button.tag = 2000; //區分同種控件的標記 這個和android 控件裏面的getId同樣 在事件裏面用來區分是那個控件的事件 spa
[button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];//對應處理的事件方法 和那個事件 日誌
[self.view addSubview:button];//把button添加到view裏面顯示
UIButton *button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect]; orm
CGRect frame2 = CGRectMake(100, 100, 100, 60); 事件
button2.frame=frame2; 開發
[button2 setTitle:@"test"forState:UIControlStateNormal]; get
button2.tag = 2001;
[button2 addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];//selector後面的方法後必定不要忘了要加個冒號 ,由於定義的buttonClicked方式後面是帶了類型爲id的參數
[ self . view addSubview :button2];-(IBAction)buttonClicked:(UIButton*)sender//事件處理的方法 sender對應的按鈕
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"title"message:@"message"delegate:self cancelButtonTitle:@"肯定"otherButtonTitles:@"otherbuttontitles", nil];//提示框 delegate 對應的是事件處理在.h 文件加上UIViewController<UIAlertViewDelegate>
switch ([sender tag]) {
case 2000:
[alert show];
break;
case 2001:
[sender setTitle:@"sender" forState:UIControlStateNormal];
break;
};
NSLog(@"button Clicked!");
}
-(void) alertView:(UIAlertView *)alertview clickedButtonAtIndex:(NSInteger)buttonIndex{//alertView 事件處理的方法 buttonIndex 對應buton的下標
NSLog(@"%@=====%d",alertview.title,buttonIndex);
}