1

ユーザーがUITextFieldに触れたときに自動的に読み込まれるUIPickerViewがあります.選択の最後に、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 に答える