单击UIButton IOS7如何设置UILabel的值

时间:2014-05-05 12:38:30

标签: ios drop-down-menu uibutton

在我的应用程序中创建了一个下拉菜单,当用户从菜单中选择选项时,所选选项必须在UILabel框中可见。为此我在UILabel内有一个UIbutton框,我已将UIButton更改为向下箭头符号,用户知道该选项在那里。

当用户点击箭头时,UIView就像POPUP一样下拉菜单UIView我保留了三个UIBut,如果用户选择了特定按钮名称的按钮至于出现在顶部的UILabel框中。为此我设置了选项UIButtons的标签每当用户点击选项时,它必须将值设置为UILabel我已经尝试过这种方法不起作用。

我的代码。

- (void) showPopView1
  {
    self.popup.alpha = 1;
   [self.popup setFrame:CGRectMake(100, 150, 150, 150)];
  }
- (IBAction)click:(id)sender {
     [self showPopView1];
     if([sender tag] == 1){   
option.text = @"You Pressed Button 1";

     }
       else if([sender tag] == 2){
       option.text = @"You Pressed Button 2";      
   }
   }

我已经使用上面的代码来完成那个不起作用的任务,请告诉我我是做对了还是我做错了。我的输出屏幕。

enter image description here

2 个答案:

答案 0 :(得分:1)

试试这个:

- (IBAction)click:(id)sender {
     [self showPopView1];

     UIButton *red = (UIButton*)[self.popUp viewWithTag:1];
     [red addTarget:self action:@selector(colorSelected:) forControlEvents:UIControlEventTouchUpInside];

     UIButton *blue = (UIButton*)[self.popUp viewWithTag:2];
     [red addTarget:self action:@selector(colorSelected:) forControlEvents:UIControlEventTouchUpInside];

    }

-(void)colorSelected : (UIButton*)sender{

     if([sender tag] == 1){   
option.text = @"You Pressed Button 1";

     }
       else if([sender tag] == 2){
       option.text = @"You Pressed Button 2";      
   }
   }

答案 1 :(得分:0)

您必须为标签创建插座,点击选项后您就可以设置:

label.text = @"Selected value";

但实际上,您的解决方案违反了人机界面指南。 Ii最好将UIPickerView用于"组合框,如"下拉列表。您甚至可以使用UITextField + UIPickerView简化解决方案。您可以为文本字段而不是键盘设置自定义视图,当您激活文本字段时 - 您的自定义视图将很好地显示。您必须在文本字段中使用属性inputView。

UIPickerView *pickerView = [[UIPickerView alloc] init];
// Set delegate and other configs for picker view
textField.inputView = pickerView;

之后当您选择文本字段时 - 选择器视图将像默认键盘一样向上滑动。您只需要处理UIPickerView委托和数据源方法来填充它并捕获选择:

– numberOfComponentsInPickerView:
– pickerView:numberOfRowsInComponent:
– pickerView:titleForRow:forComponent:
– pickerView:didSelectRow:inComponent:

应该实现此方法以确保选择器视图的正确行为。和方法

 – pickerView:didSelectRow:inComponent:
当您在选择器视图中选择任何行时,将调用

,并且您可以在那里为文本字段设置所需的值:

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
        textField.text = [NSString stringWithFormat:@"Selected row is: %d", row];
}