Search code examples
iosobjective-cuitextfielduipickerview

Setting the title of current column in UITextField from UIPickerView?


I have a UIPickerView that is being automatically loaded in loaded when a user touches the UITextField, at the end of selection the user can close the UIPicker by touching the "close" that I have added in the inputAccessoryView = toolbar;. My problem is that I need to set the value that is chosen in the UIPicker to be the value of self.textOutlet.text... I attempt to do so here but with no luck :

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    return [tempList objectAtIndex:row];
    selectedText = _textOutlet.text;

Can someone please give me some ideas on how to fix this problem? Here is my header code:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate>

@property (strong, nonatomic) NSString* selectedText; //the UITextField text



@property NSArray * tempList;




@end

And here is my implementation code:

#import "ViewController.h"

@interface ViewController ()

@property (weak, nonatomic) IBOutlet UILabel *myLabel;
@property (weak, nonatomic) IBOutlet UIPickerView *thePicker;
@property (weak, nonatomic) IBOutlet UITextField *textOutlet;


@end

@implementation T3ViewController

@synthesize tempList, selectedText;
// returns the number of 'columns' to display.
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 1;
}

// returns the # of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    return [tempList count];
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    return [tempList objectAtIndex:row];
    selectedText = _textOutlet.text;
}

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

    CGFloat chosenValue;

    switch (row) {
        case 0:
            chosenValue = 0.0000233;
            break;
        case 1:
            chosenValue = 0.0001273;
            break;
        case 2:
            chosenValue = 0.0012333;
            break;
        case 3:
            chosenValue = 0.0032204;
            break;
        case 4:
            chosenValue = 0.0234179;
            break;
        case 5:
            chosenValue = 0.0002369;
            break;
        case 6:
            chosenValue = 0.0004442;
            break;
        default:
            chosenValue = 0;
            break;
    }

    NSNumberFormatter *formatter = [[NSNumberFormatter alloc]init];
    formatter.numberStyle = NSNumberFormatterDecimalStyle;
    formatter.minimumFractionDigits=7;

    self.myLabel.text = [formatter stringFromNumber:@(chosenValue)];


}


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    tempList = [[NSArray alloc] initWithObjects:@"3222° F",@"22150° F",@"1260° F",@"12170° F",@"83210° F",@"84415° F",@"10120° F", nil];

    // hides the picker upon initiation of the view
    self.thePicker.hidden = YES;

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)doneClicked:(id) sender
{
    [_textOutlet resignFirstResponder]; //hides the pickerView
}

- (void)textFieldDidBeginEditing:(UITextField *)textField {

    if (textField == self.textOutlet) {

        self.textOutlet.inputView = _thePicker;
        self.thePicker.hidden = NO;

        UIToolbar* toolbar = [[UIToolbar alloc] init];
        toolbar.barStyle = UIBarStyleBlackTranslucent;
        [toolbar sizeToFit];

        //to make the done button aligned to the right
        UIBarButtonItem *flexibleSpaceLeft = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];


        UIBarButtonItem* doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done"
                                                                       style:UIBarButtonItemStyleDone target:self
                                                                      action:@selector(doneClicked:)];


        [toolbar setItems:[NSArray arrayWithObjects:flexibleSpaceLeft, doneButton, nil]];

        self.textOutlet.inputAccessoryView = toolbar;
    }

    }



@end

Solution

  • In your first snippet of code, you are setting selectedText after the return statement. This line of code will never get executed.