Frage

I am a big beginner in Xcode and have encountered a problem while building my app. My app consists of an UILabel, UIPickerView and UISegmentControl. I wanted my app to show a different value in UILabel every time the user changes the selected row in UIPickerView. At first I only had one set of values to show in UILabel but eventually added a second set. I added UISegmentControl in order for the user to be able to switch between these two sets. Here is an example of my "if statements"

- (void)pickerView: (UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {

if (row == 0 && segmentController.selectedSegmentIndex == 0) {


    pickLabel.text=@"100 PPM";
}

else if (row == 0 && segmentController.selectedSegmentIndex == 1) {


    pickLabel.text=@"110 PPM";
}

else if (row == 1 && segmentController.selectedSegmentIndex == 0) {


    pickLabel.text=@"95 PPM";
}

else if (row == 1 && segmentController.selectedSegmentIndex == 1) {


    pickLabel.text=@"105 PPM";
}

The problem is that the value in UILabel only changes when the selected row in UIPickerView is changed and not when the segment control is changed. For an example when I change the segment control index, UILabel does not change until I switch to a different row in the UIPicker. I want to make an IBAction that forces the UILabel to change when the segment control index is changed. Than I will connect my IBAction to the UISegmentControl with "value changed". I need some code to put into the IBAction. Please help!

War es hilfreich?

Lösung

- (void)pickerView: (UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {

get executed when you change the picker value alone .When changing the segment you have to invoke a method and do the same here in this method

Try this

declare

NSInteger selectedRowInpicker;

and use

-(IBAction)valueChanged
{
    if (selectedRowInpicker == 0 && segmentController.selectedSegmentIndex == 0) {


        pickLabel.text=@"100 PPM";
    }

    else if (selectedRowInpicker == 0 && segmentController.selectedSegmentIndex == 1) {


        pickLabel.text=@"110 PPM";
    }

    else if (selectedRowInpicker == 1 && segmentController.selectedSegmentIndex == 0) {


        pickLabel.text=@"95 PPM";
    }

    else if (selectedRowInpicker == 1 && segmentController.selectedSegmentIndex == 1) {


        pickLabel.text=@"105 PPM";
    }
}


- (void)pickerView: (UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    selectedRowInpicker =row;
    [self valueChanged];
}

connect the valueChanged to the segment action "value changed"

Andere Tipps

you should register an event for UISegmentedControl as well.

[segmentedControl addTarget:self action:@selector(valueChanged:) forControlEvents: UIControlEventValueChanged];

now create a method like:

-(void) valueChanged:(UISegmetedControl *)control
{
    [self modifyLabel];
}

- (void)pickerView: (UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    [self modifyLabel];
}

-(void) modifyLabel
{
//Based on picker and segmented controller, update the label
}   
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top