gpt4 book ai didi

ios - 从 UIPickerView 设置 UITextField 中当前列的标题?

转载 作者:行者123 更新时间:2023-12-01 19:09:22 24 4
gpt4 key购买 nike

我有一个 UIPickerView,当用户触摸 UITextField 时,它会自动加载,在选择结束时,用户可以通过触摸我在 inputAccessoryView = toolbar; 中添加的“关闭”来关闭 UIPicker。 .我的问题是我需要将 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

最佳答案

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

关于ios - 从 UIPickerView 设置 UITextField 中当前列的标题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17602631/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com