gpt4 book ai didi

ios - 从一个 View 到另一 View 更新标签内容

转载 作者:行者123 更新时间:2023-11-29 03:09:19 30 4
gpt4 key购买 nike

我是 iOS 开发的新手,我知道有人问过这样的问题,但我似乎无法从这些示例中提取出我认为我需要的东西。

我正在尝试通过编写一个非常简单的抵押贷款计算器来自学这一点。我有两个 View ,MortgageCalculatorViewController,用户在其中输入贷款值(value)、期限和利息;和 ResultsViewController,在其中重新显示此信息,并在标签中按月付款。我目前的问题是我似乎无法弄清楚如何将计算出的每月付款值传递给 ResultsViewController。

我将 segue 命名为 showResultsSegue。

在 MortgageCalculatorViewController 中:

#import "MortgageCalculatorViewController.h"
#import "ResultsViewController.h"

@interface MortgageCalculatorViewController ()

@end

@implementation MortgageCalculatorViewController

NSString *paymentText;

-(IBAction)calculateMonthlyPayment
{
float rate = (self.interestRate.text.floatValue / 12) / 100;
long term = self.termInYears.text.integerValue;
float principle = self.loanAmount.text.floatValue;

float termedRate = pow((1 + rate), term);
float payment;

payment = (principle * rate * termedRate) / (termedRate - 1);

paymentText = [NSString stringWithFormat:@"%f", payment];
}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if([segue.identifier isEqualToString:@"showResultsSegue"]){
ResultsViewController *controller = (ResultsViewController *)segue.destinationViewController;
controller.paymentLabel.text = paymentText;
}
}

@end

在 ResultsViewController 中:

@interface ResultsViewController : UIViewController

@property (nonatomic) IBOutlet UILabel *loanAmountLabel;
@property (nonatomic) IBOutlet UILabel *termInYearsLabel;
@property (nonatomic) IBOutlet UILabel *interestRateLabel;
@property (nonatomic) IBOutlet UILabel *paymentLabel;

-(IBAction)close;

@end

我已经根据我在 Passing Data between View Controllers 中找到的信息指导了这种方法,但在转场之后我仍然没有看到任何变化。

如有任何帮助,我们将不胜感激。谢谢。

最佳答案

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if([segue.identifier isEqualToString:@"showResultsSegue"]){
ResultsViewController *controller = (ResultsViewController *)segue.destinationViewController;
controller.paymentLabel.text = paymentText;
}
}

准备转场时, View 可能尚未加载,因此 paymentLabelnil。相反,在 ResultsViewController 上声明一个 paymentText 属性,并将该值分配给 viewDidLoad 中的 paymentLabel

prepareForSegue:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if([segue.identifier isEqualToString:@"showResultsSegue"]){
ResultsViewController *controller = (ResultsViewController *)segue.destinationViewController;
controller.paymentText = paymentText;
}
}

ResultsViewController的实现:

@interface ResultsViewController : UIViewController

@property(copy, nonatomic) NSString *paymentText;

@end

@implementation ResultsViewController

- (void)viewDidLoad {
[super viewDidLoad];

self.paymentLabel.text = self.paymentText;
}

@end

关于ios - 从一个 View 到另一 View 更新标签内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22388757/

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