在我的应用程序中,当用户从菜单中选择选项时,我的应用程序创建了一个下拉菜单,所选选项必须在 UILabel
框中可见。为此,我在 UIbutton
上保留了一个 UILabel
框,并且我已将 UIButton 更改为向下箭头符号,以便用户知道该选项在那里。
当用户单击箭头时,UIView
会以 POPUP
的形式出现,就像 UIView
中的下拉菜单一样,我保留了三个 UIButton如果用户选择特定按钮名称的按钮,该按钮名称将显示在顶部的 UILabel 框中。为此,每当用户根据必须将值设置为 UILabel
的标签单击选项时,我已经为 Options UIButtons 设置了标签我已经尝试过这种方法它不起作用。
我的代码。
- (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";
}
}
我已经使用上面的代码来完成该任务,但它不起作用,请告诉我我做对了还是哪里做错了。我的输出屏幕。
最佳答案
试试这个:
- (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";
}
}
关于ios - 如何通过单击 UIButton IOS7 来设置 UILabel 的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23472673/