2013-07-11 44 views
1

我有一個UIPickerView,當用戶觸摸UITextField時會自動加載,在選擇結束時用戶可以通過觸摸我在inputAccessoryView = toolbar;中添加的「close」來關閉UIPicker。我的問題是,我需要設置選擇在UIPicker價值是self.textOutlet.text價值...我嘗試這樣在這裏做,但沒有運氣:從UIPickerView設置UITextField中當前列的標題?

- (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.0; 
      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",@"° 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 

回答

2

在你的代碼的第一個片段,你return語句後設置selectedText。這行代碼永遠不會被執行。

+0

謝謝!愚蠢的錯誤 – vzm

1

您需要返回後設置selectedText

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