在Viewcontroller上进行一些计算,需要将结果显示给另一个Viewcontroller

时间:2013-12-10 20:35:13

标签: ios uiviewcontroller

我有一个应用程序,在那里进行一些计算,需要将此计算结果传输到ViewController3并在那里显示结果。现在我在同一个ViewController2中使用label,我在那里进行计算。谢谢你的帮助。

ViewController2.h

#import <UIKit/UIKit.h>

@interface ViewController2 : UIViewController<UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UILabel *gasPrice;

@property (weak, nonatomic) IBOutlet UILabel *gasCarMileage;

@property (weak, nonatomic) IBOutlet UITextField *perGalon;

@property (weak, nonatomic) IBOutlet UITextField *miles;

@property(nonatomic, copy, readonly) NSString *result;

- (IBAction)getIt:(id)sender;

@end

ViewController2.m

#import "ViewController2.h"

#import "ViewController3.h"


@interface ViewController2 ()

@end

@implementation ViewController2

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
    // Custom initialization
}
return self;
}

//Start calculation

- (IBAction)getIt:(id)sender; {

float perGalon = ([_perGalon.text floatValue]);

float miles = ([_miles.text floatValue]);

float mileCost = perGalon / miles;

[self performSegueWithIdentifier:@"viewController3" sender: nil];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"viewController3"]) {
    ViewController3 *viewController3 = [segue destinationViewController];
    viewController3.result = [[NSString alloc] initWithFormat: @"Every mile you drive
will cost you $ %f", mileCost];   
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.perGalon.delegate = self;
self.miles.delegate = self;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    return [textField resignFirstResponder];
}
@end

ViewController3.h

#import <UIKit/UIKit.h>

@interface ViewController3 : UIViewController

@property(nonatomic, copy) NSString *result;

@end

ViewController3.m

#import "ViewController3.h"
#import "ViewController2.h"

@interface ViewController3 ()

@end

@interface ViewController2 ()

@property(nonatomic, copy, readwrite) NSString *result;

@end

@implementation ViewController3

- (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.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end

3 个答案:

答案 0 :(得分:0)

您可以在View Controller 3中定义一个字符串属性,当您从视图控制器2推送视图控制器3时,您将设置该属性。

ViewController3接口声明

@interface ViewController2 ()

@property(nonatomic, copy) NSString *result;

@end

在ViewController 2中,您将在getIt()方法的最后一行实现此行。

[self performSegueWithIdentifier:@"viewController3" sender: nil]

并实现另一个名为prepareForSegue的方法,如下所示

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"viewController3"]) {
        ViewController3 *viewController3 = [segue destinationViewController];
        viewController3.result = "YOUR CALCULATION RESULT HERE"
    }
}

请记住,您必须为视图控制器3设置标识符,转到故事板并选择视图控制器3的视图,并选择On标识检查器,将指定的Storyboard ID设置为“viewController3”。

答案 1 :(得分:0)

使用协议, 在ViewController2.h中

@protocol ViewController2Delegate;

@property (strong, nonatomic) id<ViewController2Delegate> delegate;


@protocol BexAPIClientDelegate <NSObject>
- (void)ViewController2:(ViewController2 *)vc didCalculateCost:(CGFloat)cost;
@end


Then at the end of the getIt() call the delegate 
[self.delegate viewController2:self didCalculateCost:gasCostPerMile];

加载时,您需要将ViewController3设置为ViewController2的委托。此外,您还需要设置ViewController3以符合协议。

使用该协议是值得的,因为它将使您的代码清晰易管。

答案 2 :(得分:-1)

在VC3中保留一个字符串。初始化VC3对象时,请从VC2设置值。更新VC3中的标签(在viewDidLoad方法中)