Domanda

In my application have created a drop down Menu when user select the option form the menu the selected option must be visible in the UILabel box. For that i have one UILabel box inside that i have kept on UIbutton and i have changed the UIButton into down arrow symbol to user know the option is there.

When user click on the arrow a UIView comes as a POPUP like a drop down Menu inside the UIView i have kept the three UIButton if user select the button that particular button name as to appear in the UILabel box in the top. For that i have set the Tags for the Options UIButtons whenever user click option according to the tag it has to set the Value to UILabel i have tried this method its not working.

My code.

- (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";      
   }
   }

I have used the above code to achieve that task its not working please tell me whether I'm doing the right or where I'm doing wrong . My Output Screen.

enter image description here

È stato utile?

Soluzione

Try this :

- (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";      
   }
   }

Altri suggerimenti

You have to create outlet for your label, and after clicking on an option you will be able to set:

label.text = @"Selected value";

But actually, your solution is going against Human Interface Guideline. Ii is better to use UIPickerView for "combo box like" drop-down list. You can even simplify solution using UITextField + UIPickerView. You can set a custom view for text field instead of keyboard, and when you activate the text field - your custom view will appear nicely. You have to use property inputView in text field.

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

After that when you select a text filed - picker view will swipe up just like default keyboard does. You just need to handle UIPickerView delegate and data source methods to fill it and catch the selection:

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

This methods should be implemented for correct behavior of picker view. And method

 – pickerView:didSelectRow:inComponent:

will be called when you select any row in picker view, and you can set needed value for text field there:

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
        textField.text = [NSString stringWithFormat:@"Selected row is: %d", row];
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top