天天看點

UI02_UIButton

總結

前期準備工作與UILabel相同
           
建立一個Button
  ()Button用自己的便利構造器的方式來建立對象,而且button不用release
  ()UIButton *button=[UIButton buttonWithType:UIButtonTyprSystem];
           
2.指定Button的位置和大小
button.frame=CGRectMake(100,100,100,40);
           
3.設定背景顔色
button.backgroundColor=[UIColor redColor];
           
将button挂在window上
[self.window addSubview:button];
           
.設定标題
[button setTitle:@"确認"forState:UIControlStateNormal];
           
設定标題字型大小
button.titleLabel.font=[UIFont systemFontOfSize:];
           
設定圓角
button.layer.cornerRadius=;
           
設定邊框
button.layer.borderWidth=;
           
點選方法
  這是button最重要的部分  sel是方法選擇器   首先是@select
  [button addTarget:self action:@selector(click:)forControlEvents:UIControlEventTouchUpInside]; 
  建立click方法
  -(void)click:(UIButton *)button
  {
    NSLog(@"點選");
  }
           
更換button内容

  UIButton *button=[UIButton buttonWithType:UIButtonTypeSystem];
  button.frame=CGRectMake(,,,);
  [self.window addSubview:button];
  [button setTitle:@"确認"forState:UIControlStateNormal];
  button.layer.cornerRadius=;
  button.layer.borderWidth=;
  [button addTarget:self action:@selector(click:)forControlEvents:UIControlEventTouchUpInside];
  //設定tag就是為後面的父類找到對應的子類,友善對子類進行設定
 button.tag=;

  click方法實作部分
{
//等号後面加(UIButton *)的原因是window本身是UIView.雖然有繼承關系,但是為了類型統一我們還是在此處對他進行類型的轉換.保證類型的統一.
UIBuuton *but=(UIBuuton *)[self.window viewWithTag:];
//判斷目前按鈕的标題
//擷取目前按鈕的标題
NSLog(@"%@",but.currentTitle);//點選時擷取button的内容
if([button.currentTitle isEqualToString:@"确認"])
{
[but setTiltle:@"取消"forState:UIControlStateNormal];

}
}
 //将确認改成取消 
           
點選更換圖檔
前述:
     ()給button設定圖檔用image(圖檔)
     ()圖檔大小會自動适應button的設定邊框,邊框多大,圖檔就多大
     ()将圖檔添加方法裡不是像OC拽檔案那樣取得路徑就可以了,而是把圖檔名字輸入到裡面

設定一個屬性BOOL類型
@property (nonatomic,assign)BOOL isClick;
實作部分:
UIButton *button=[UIBuutton buttonWithtype:UIButtonTypeSystem];
button.frame=CGRectMake(,,,);
[self.window addSubview:button];
[button addTarget:self action:@selector(change Pic:)forControlEvents:UIControlEventTouchUpInside];
[button setImage:[UIImage imageNamed:@"BtfOff.png"]forState:UIControlStateCustom];
//如果想使用setImage設定圖檔的話.button的類型要調整成custom.setImage方法不會把圖檔方法成按鈕大小

changePic方法:更換按鈕背景圖
-(void)changeImage:(UIButton *)button{
//判斷
if(self.isClick){
[button seyImage:[UIImage imageNamed:@"BtnOn.png"]forState:UIContolStateNormal];
}else{
[button setImage:[UIImage imageNamed:@"BtnOff.png"forState:UIControlStateNormal];
     }
 //對目前的狀态進行調整
self.isClick=!self.isClick;
}

//按鈕的點選方法會傳過來一個相應類型的button.誰觸犯了這個點選方法,相應的button就是那個對象