1

我有一个 UIPickerView,当用户触摸 UITextField 时,它会自动加载,在选择结束时,用户可以通过触摸我在inputAccessoryView = toolbar;. 我的问题是我需要将在 UIPicker 中选择的值设置为self.textOutlet.text... 我尝试在这里这样做但没有运气:

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

有人可以给我一些关于如何解决这个问题的想法吗?这是我的标题代码:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate>

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



@property NSArray * tempList;




@end

这是我的实现代码:

#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
4

2 回答 2

2

在您的第一段代码中,您selectedText在 return 语句之后进行设置。这行代码永远不会被执行。

于 2013-07-11T20:19:57.317 回答
1

您需要在设置 selectedText 之后返回

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
selectedText = _textOutlet.text;
return [tempList objectAtIndex:row];
于 2013-07-11T20:20:37.323 回答