你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> ios經過代碼創立按鈕

ios經過代碼創立按鈕

編輯:IOS開發綜合

辦法一:

-(void) viewDidLoad{
    [super viewDidLoad];
    UIButton *addbtn = [[UIButton alloc] init];
    [addbtn setBackgroundImage:[UIImage imageNamed:@"btn_1"] forState:UIControlStateNormal];
    [addbtn setBackgroundImage:[UIImage imageNamed:@"btn_2"] forState:UIControlStateHighlighted];
    addbtn.frame = CGRectMake(90, 50, 20, 50);//(x,y,w,h)
    [self.view addSubview:addbtn];
    //監聽btcn
    [addbtn addTarget:self action:@selector(testAdd) forControlEvents:UIControlEventTouchUpInside];

}

//點擊事情調用的辦法
-(void)testAdd{
    NSLog(@">>>>>>Add Ok!");
}

辦法二:

#pragma mark 相反辦法共用(創立多個按鈕時)
-(void) AddBtn:(NSString *)Normal Highlighted:(NSString *) Highlighted frame:(CGRect)frame
         action:(SEL)action{
    UIButton *btn = [[UIButton alloc] init];c
    [btn setBackgroundImage:[UIImage imageNamed:Normal] forState:UIControlStateNormal];
    [btn setBackgroundImage:[UIImage imageNamed:Highlighted] forState:UIControlStateHighlighted];
    btn.frame = frame;
    //監聽btn
    [btn addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];

}

-(void) viewDidLoad{
    [super viewDidLoad];
    [self AddBtn:@"btn_1" Highlighted:@"btn_2" frame: CGRectMake(90, 50, 50, 50) action:@selector(testAdd)];
    
    [self AddBtn:@"btn_2" Highlighted:@"btn_1" frame: CGRectMake(180, 50, 50, 50) action:@selector(testTwo)];
}

//點擊事情調用的辦法
-(void)testAdd{
    NSLog(@">>>>>>Add Ok!");
    [self Show];
}

-(void)testTwo{
    NSLog(@">>>>>>Two");
    [self Show];
}

-(void)Show{
    NSLog(@">>>>Age!");
}





【ios經過代碼創立按鈕】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved