2017-03-03 71 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 .. –

相关问题