2017-03-03 58 views
-1

我正在研究有關按鈕單擊的功能,其中我的要求是我只有一個按鈕,如果單擊按鈕第一次添加標籤,如果單擊相同的按鈕第二次添加另一個標籤,例如5次按鈕被點擊5個標籤應該被添加。單擊按鈕時添加文本文件Objective-C

-(IBAction) btnAddClicked: (id) sender { 
    if (_btnAdd.tag == 0) { 
     _lblAdd1.hidden = YES; 
    } 
    if (_btnAdd.tag == 1) { 
     _lblAdd2.hidden = YES; 
    } 
    if (_btnAdd.tag == 2) { 
     _lblAdd3.hidden = YES; 
    } 
    if (_btnAdd.tag == 3) { 
     _lblAdd4.hidden = YES; 
    } 
} 
+0

請顯示你已經嘗試過! – Rikh

+0

你已經在stroyboard中有5個標籤了嗎? –

+0

是的,我需要用按鈕點擊來顯示它們 – Abhinav

回答

0

您必須確定哪個按鈕是發送事件

-(IBAction)btnAddClicked: (id) sender { 
    //Typecast to retrieve tag of current button. 
    UIButton *btn = (UIButton *) sender; 
    if (btn.tag == 0) { 
     _lblAdd1.hidden = YES; 
    } 
    if (btn.tag == 1) { 
     _lblAdd2.hidden = YES; 
    } 
    if (btn.tag == 2) { 
     _lblAdd3.hidden = YES; 
    } 
    if (btn.tag == 3) { 
     _lblAdd4.hidden = YES; 
    } 
} 
+0

我不明白,總是執行btn.tag == 0 – Abhinav

+0

這隻意味着你沒有正確設置按鈕標記。 – Teffi

0

您可以根據clicks.if你想要一個以上的可以繼續,否則最後點擊設置按鈕,標籤作爲更新標籤0

- (IBAction爲)btnAddClicked:(ID)發送方{

的UIButton * BTN =(的UIButton *)發送者;

if (btn.tag == 0) { 
    _lblAdd1.hidden = YES; 
    btn.tag = 1; 
} 
else if (btn.tag == 1) { 
    _lblAdd2.hidden = YES; 
    btn.tag = 2; 
} 
else if (btn.tag == 2) { 
    _lblAdd3.hidden = YES; 
    btn.tag = 3; 
} 
else if (btn.tag == 3) { 
    _lblAdd4.hidden = YES; 
    btn.tag = 0; 
} 
0

下面的代碼是動態文本框創建的n個:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
return _count; } 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
TextViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath]; 
return cell; } 

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{ 
UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 30)]; 
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 30)]; 
[button setTitle:@"Add TextField" forState:UIControlStateNormal]; 
button.backgroundColor = [UIColor blueColor]; 
[button addTarget:self action:@selector(addTextField) forControlEvents:UIControlEventTouchUpInside]; 
[footerView addSubview:button]; 
return footerView; } 

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{ 
return 30; } 

-(void)addTextField{ 
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:_count inSection:0]; 
_count++; 
[self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; } 
+0

問題標記爲_objective-c_而不是_swift_。 – Teffi

0

到的方式來做到這一點 1使用的TableView誰的單元格中包含textFld,每次當你點擊該按鈕,更新UITableView By Number of cells of last last count .. 示例:首先,單擊將indexPath/number/etc添加到可變數組中,並重新加載tableview以查看該數組的計數,表格單元格將爲一個單元格創建.. 現在,在第二次單擊時,再向該數組中添加一個,然後重新加載tableview,現在是tableview將爲陣列計數創建單元,現在是2 ... SO。希望你得到它

+0

2.使用UIStackView,並在每次點擊創建一個UITextFld和它作爲該子視圖的StackView .. –

相關問題