0

我设置了 2 组 Objective-C 类(没有 xib 文件)

  1. AdultTicketCalculatorViewController

//.h 文件

#import <UIKit/UIKit.h>

@interface AdultTicketCalculatorViewController : UIViewController {

    IBOutlet UITextField *Quantity;
    IBOutlet UILabel *Pricelabel;
    IBOutlet UILabel *TotalLabel;
}

-(IBAction)calculate;
-(IBAction)clear;

@end

//.m

#import "AdultTicketCalculatorViewController.h"

@interface AdultTicketCalculatorViewController ()

@end

@implementation AdultTicketCalculatorViewController

-(IBAction)calculate {

    float x = ([Quantity.text floatValue]);
    float c = x*15;

    TotalLabel.text = [[NSString alloc] initWithFormat:@"$%.2f", c];

}

-(IBAction)clear {

    Quantity.text =@"";
    Pricelabel.text =@"$15.00";
    TotalLabel.text =@"";

}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

    [Quantity resignFirstResponder];

}
  1. AdultPayNowViewController

//。H

#import <UIKit/UIKit.h>

@interface AdultPayNowViewController : UIViewController {

    IBOutlet UILabel *Quantity ,*Price ,*QuantitySpace ,*TotalSum ,*TotalSumSpace;
    IBOutlet UIButton *ConfirmPurchase;

}

@end

//.m(我还没有实现任何东西)

我想从 AdultTicketCalculator 中获取一些用户输入和计算值,并以下列方式在 AdultPayNowViewController 中显示它们:

  1. UITextField 'Quantity' (AdultTicketCalculatorViewController) 出现在 UILabel 'QuantitySumSpace (AdultPayNowViewController)
  2. UILabel 'TotalLabel' (AdultTicketCalculatorViewController) 出现在 UILabel 'TotalSumSpace'
4

1 回答 1

0

您可以通过使用两种不同的方式来实现这一目标。

1.将您想要的数据保存在AdultTicketCalculatorViewController此类中NSUserDefaults并检索数据AdultPayNowViewController

像这样 :

whenever pushing your view place this code or in 
- (void)viewDidDisappear:(BOOL)animated{
//place here
}

在 AdultTicketCalculatorViewController

NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
    [defaults setObject: TotalLabel.text forKey:@"TotalLabel"];
    [defaults setObject: Quantity.text forKey:@"Quantity"];
    [defaults synchronize];

并在 viewWillAppear 方法中重新分配文本字段值。

AdultPayNowViewController:

在 viewWillAppear 方法中放置以下代码。

NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
   TotalSumSpace.text= [defaults  valueForKey:@"TotalLabel"];
   QuantitySumSpace.text= [defaults  valueForKey:@"Quantity"];

2.从 中合成字符串对象AdultPayNowViewController并分配值AdultTicketCalculatorViewController

于 2013-03-28T11:48:25.030 回答