从另一个视图更新标签内容

时间:2014-03-13 19:18:43

标签: ios objective-c uiviewcontroller segue

我对iOS开发非常陌生,我知道有这样的问题,但我似乎无法从这些示例中提取出我认为需要的内容。

我试图通过编写一个非常简单的抵押贷款计算器来教给我自己。我有两个视图,MortgageCalculatorViewController,用户输入贷款价值,期限和利息;和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中找到的信息引导了这种方法,但是在segue之后我仍然没有看到任何变化。

任何帮助将不胜感激。感谢。

3 个答案:

答案 0 :(得分:1)

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

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

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

答案 1 :(得分:0)

您的ResultsViewController可能尚未初始化其视图元素,包括paymentLabel。在设置标签文本之前尝试显式调用view,如下所示:

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

调用view会导致视图控制器加载其视图元素,从而让您正确设置标签的文本。

答案 2 :(得分:0)

到目前为止,最简单的原因是,如果不使用代理,单身或使用prepareForSegue,如上所述,使用NSUserDefaults。

保存字符串:

[[NSUserDefaults standardUserDefaults] setObject:paymentText forKey:@"payment"];
[[NSUserDefaults standardUserDefaults]synchronize];

阅读字符串:

NSString *readPaymentString = [[NSUserDefaults standardUserDefaults] stringForKey:@"payment"];

你基本上需要做的是使用我在一个视图中提供的保存代码和第二个视图中的读取代码,简单。

我希望这是有道理的。