ios – 如何让UIPickerview自动更新UILabel

ios – 如何让UIPickerview自动更新UILabel,第1张

概述我有一个转换器应用程序,我希望它在我输入数字(应该转换)到文本字段时更新标签.我想让它自动更新,所以我不需要再次选择轮子上的单位进行更新. 这是我的代码: - (void)viewDidLoad{ [super viewDidLoad]; { [super viewDidLoad]; // Do any additional setup after loading th 我有一个转换器应用程序,我希望它在我输入数字(应该转换)到文本字段时更新标签.我想让它自动更新,所以我不需要再次选择轮子上的单位进行更新.

这是我的代码:

- (voID)vIEwDIDLoad{   [super vIEwDIDLoad];  {    [super vIEwDIDLoad];    // Do any additional setup after loading the vIEw,typically from a nib.    _convertFrom = @[@"MTPA",@"MMcf/day",@"Mill.Sm3/day",@"MMBTU",@"Boe/day"];    _convertRates = @[ @1.0f,@133.4246575f,@3.780821918f,@142465.7534f,@23780.8f];    _convertTo = @[@"MTPA",@23780.8f];}}

我在这里干什么?

- (voID)dIDReceiveMemoryWarning{[super dIDReceiveMemoryWarning];

}

-(IBAction)textFIEldReturn:(ID)sender{    [sender resignFirstResponder];}-(IBAction)backgroundtouched:(ID)sender{    [inputText resignFirstResponder];}#pragma mark -#pragma mark PickerVIEw DataSource - (NSInteger)numberOfComponentsInPickerVIEw:(UIPickerVIEw *)pickerVIEw{return 2;} - (NSInteger)pickerVIEw:(UIPickerVIEw *)pickerVIEw numberOfRowsInComponent:      (NSInteger)component  { if (component == 0)  {    return [_convertFrom count];}return [_convertTo count];} - (Nsstring *) pickerVIEw: (UIPickerVIEw *)pickerVIEw          TitleForRow:(NSInteger)row         forComponent:(NSInteger)component{if (component == 0) {    return [_convertFrom objectAtIndex:row];}return [_convertTo objectAtIndex:row];} #pragma mark - #pragma mark PickerVIEw Delegate -(voID)pickerVIEw:(UIPickerVIEw *)pickerVIEw dIDSelectRow:(NSInteger)row   inComponent:(NSInteger)component { float convertFrom = [[_convertRates objectAtIndex:[pickerVIEw selectedRowInComponent:0]]     floatValue]; float convertTo = [[_convertRates objectAtIndex:[pickerVIEw selectedRowInComponent:1]] floatValue];float input = [inputText.text floatValue];float to = convertTo;float from = convertFrom;float convertValue = input;float relative = to / from;float result = relative * convertValue;Nsstring *convertFromname = [_convertFrom objectAtIndex:[pickerVIEw selectedRowInComponent:0]];Nsstring *convertToname = [_convertFrom objectAtIndex:[pickerVIEw selectedRowInComponent:1]];Nsstring *resultString = [[Nsstring alloc]initWithFormat:                          @" %.4f %@ = %.4f %@",convertValue,convertFromname,result,convertToname];resultLabel.text = resultString; }
解决方法 您的问题与选择器视图无关.您希望监听文本字段的更改,并根据输入到文本字段中的最新文本更新转换和标签.

>设置在值更改时在文本字段上调用的方法.
>选择器视图委托方法中的所有代码计算转换并更新标签需要放在另一个可以从两个地方调用的方法中.
>在对文本字段中的更改做出反应的新方法中,调用刚刚创建的执行转换的方法并更新标签.

因此,此时您的选择器视图委托方法和文本字段更改方法都会调用从选择器获取转换的方法和文本字段中的文本,然后执行转换,最后更新标签.

编辑:更多细节

重构当前的选择器视图委托方法,如下所示:

-(voID)pickerVIEw:(UIPickerVIEw *)pickerVIEw dIDSelectRow:(NSInteger)row inComponent:(NSInteger)component {    [self updateConversionLabel];}- (voID)updateConversionLabel {    float convertFrom = [[_convertRates objectAtIndex:[pickerVIEw selectedRowInComponent:0]] floatValue];    float convertTo = [[_convertRates objectAtIndex:[pickerVIEw selectedRowInComponent:1]] floatValue];    float input = [inputText.text floatValue];    float to = convertTo;    float from = convertFrom;    float convertValue = input;    float relative = to / from;    float result = relative * convertValue;    Nsstring *convertFromname = [_convertFrom objectAtIndex:[pickerVIEw selectedRowInComponent:0]];    Nsstring *convertToname = [_convertFrom objectAtIndex:[pickerVIEw selectedRowInComponent:1]];    Nsstring *resultString = [[Nsstring alloc]initWithFormat:        @" %.4f %@ = %.4f %@",convertToname];    resultLabel.text = resultString; }

然后,您需要创建一个方法来处理对文本字段的更改:

- (voID)textFIEldChanged:(UITextFIEld *)textFIEld {    [self updateConversionLabel];}

现在在IB中,将textFIEldChanged:方法连接到inputText文本字段的“value changed”事件.我不使用IB,所以我不知道这样做的确切术语.

总结

以上是内存溢出为你收集整理的ios – 如何让UIPickerview自动更新UILabel全部内容,希望文章能够帮你解决ios – 如何让UIPickerview自动更新UILabel所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1008165.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存