使用prepareForSegue在视图控制器之间传递值:Objective-C

时间:2015-11-08 00:08:48

标签: ios objective-c uiviewcontroller segue

我知道这已经在这里被问过并且我已经使用了它的代码但我仍然​​无法将double grandTotal(从InitialStoreViewController)的值传递给double isSomethingEnabled(在ViewController中)。这是我的视图控制器文件:

InitialStoreViewController.h

#import <UIKit/UIKit.h>
#import "InitialStoreViewController.h"
#import "ViewController.h" 

@interface InitialStoreViewController : UITableViewController<UITextFieldDelegate>
{
    // TargetViewCon TargetViewController *targetView;
    IBOutlet UILabel *peasLabel;
    IBOutlet UILabel *eggsLabel;
    IBOutlet UILabel *milkLabel;
    IBOutlet UILabel *beansLabel;

    // requested amounts of each product 
    int peasAmountInt; 
    int eggsAmountInt; 
    int milkAmountInt; 
    int beansAmountInt;

    double peasTotal;
    double eggsTotal;
    double milkTotal;
    double beansTotal;

    double grandTotal;
}

- (IBAction)peasStepper:(UIStepper *)sender;
- (IBAction)eggsStepper:(UIStepper *)sender;
- (IBAction)milkStepper:(UIStepper *)sender;
- (IBAction)beansStepper:(UIStepper *)sender;
- (IBAction)calcTotal:(UIBarButtonItem *)sender;

@property double price; 
@property double priceToPass;

//this method is  
//- (void) stepperAction: (UIStepper*)stepper toLabel: (UILabel*)label;

@end

InitialStoreViewController.m

#import "InitialStoreViewController.h"
#import "ViewController.h"
#import "Product.h"

@implementation InitialStoreViewController

- (void)viewDidLoad 
{

    //sunday:  
    [super viewDidLoad];

}

- (IBAction)peasStepper:(UIStepper *)sender 
{
    peasAmountInt = (int) sender.value;
    NSLog(@"Peas Amount: %i", peasAmountInt);
    peasLabel.text = [NSString stringWithFormat:@"%i", peasAmountInt];

    //Initiating object of class Product
    Product *peas = [[Product alloc]init];
    peas.amountInt = peasAmountInt;
    peasTotal = [peas multiplyAmount: peas.amountInt byPrice:0.95];
    NSLog(@"Total amount for peas: %f", peasTotal);

    grandTotal = peasTotal + eggsTotal + milkTotal + beansTotal;
    NSLog(@"TOTAL Amount: %f", grandTotal);
}

- (IBAction)eggsStepper:(UIStepper *)sender 
{
    eggsAmountInt = (int) sender.value;
    NSLog(@"Eggs Amount: %i", eggsAmountInt);
    eggsLabel.text = [NSString stringWithFormat:@"%i", eggsAmountInt];

    //Initiating object of class Product
    Product *eggs = [[Product alloc]init];
    eggs.amountInt = eggsAmountInt;
    eggsTotal = [eggs multiplyAmount: eggs.amountInt byPrice:2.10];
    NSLog(@"Total amount for eggs: %f", eggsTotal);

    grandTotal = peasTotal + eggsTotal + milkTotal + beansTotal;
    NSLog(@"TOTAL Amount: %f", grandTotal);
}

- (IBAction)milkStepper:(UIStepper *)sender 
{
    milkAmountInt = (int) sender.value;
    NSLog(@"Milk Amount: %i", milkAmountInt);
    milkLabel.text = [NSString stringWithFormat:@"%i", milkAmountInt];

    //Initiating object of class Product
    Product *milk = [[Product alloc]init];
    milk.amountInt = milkAmountInt;
    milkTotal = [milk multiplyAmount: milk.amountInt byPrice:1.30];
    NSLog(@"Total amount for milk: %f", milkTotal);

    grandTotal = peasTotal + eggsTotal + milkTotal + beansTotal;
    NSLog(@"TOTAL Amount: %f", grandTotal);
}

- (IBAction)beansStepper:(UIStepper *)sender 
{
    beansAmountInt = (int) sender.value;
    NSLog(@"Beans Amount: %i", beansAmountInt);
    beansLabel.text = [NSString stringWithFormat:@"%i", beansAmountInt];

    //Initiating object of class Product
    Product *beans = [[Product alloc]init];
    beans.amountInt = beansAmountInt;
    beansTotal = [beans multiplyAmount: beans.amountInt byPrice:1.30];
    NSLog(@"Total amount for beans: %f", beansTotal);

    grandTotal = peasTotal + eggsTotal + milkTotal + beansTotal;
    NSLog(@"TOTAL Amount: %f", grandTotal);


}

- (IBAction)calcTotal:(UIBarButtonItem *)sender 
{

    grandTotal = peasTotal + eggsTotal + milkTotal + beansTotal;
    NSLog(@"TOTAL Amount: %f", grandTotal);

}


//sunday

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if([segue.identifier isEqualToString:@"showDetailSegue"]){
        UINavigationController *navController = (UINavigationController *)segue.destinationViewController;
        ViewController *controller = (ViewController *)navController.topViewController;
        // controller.isSomethingEnabled = YES;
         controller.isSomethingEnabled = &(grandTotal);
    }
}

@end

ViewController.h

#import <UIKit/UIKit.h>
#import "InitialStoreViewController.h"

@interface ViewController : UIViewController <UIPickerViewDataSource,
UIPickerViewDelegate> 
{

    NSArray *news;
    NSMutableData *data;
    double isSomethingEnabled;

} 

@property(nonatomic) double *totalLabelDouble;
@property(nonatomic) double *isSomethingEnabled;
@property (nonatomic, strong) NSArray *currencyArray;
@property (weak, nonatomic) IBOutlet UILabel *totalLabel;
@property (weak, nonatomic) IBOutlet UIPickerView *picker;

@end

ViewController.m

#import "ViewController.h"
#import "Product.h"
#import "InitialStoreViewController.h" 

@interface ViewController ()

@end

@implementation ViewController

//double totalLabelDouble = double isSomethingEnabled;

@synthesize currencyArray, totalLabel, picker;

- (void)viewDidLoad 
{
    [super viewDidLoad];

    //checking if value of grandTotal was passed to this view controller:   
    NSLog(@"HHHHHHHHHHHHHHHHHHHHHHHHHHHH :%f", isSomethingEnabled);

    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;

    NSURL *url = [NSURL URLWithString:@"http://www.apilayer.net/api/live?access_key=0b8125d8ca8e2801643e360440409165"];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [[NSURLConnection alloc] initWithRequest:request delegate:self];

    // Do any additional setup after loading the view, typically from a nib.
    //connection:willCacheResponse
    //connection:didReceiveResponse
    //connection:didReceiveData
    //connectionDidFinishLoading
    //connection:didFailWithError 
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
{
    data = [[NSMutableData alloc] init]; 
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)theData 
{
    [data appendData:theData]; 
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
{
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
    news= [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
    //[mainTableView reloadData];
    NSLog(@"RAW DATA Marcin %@", data);

    //NEW Stuff:

    // Now create a NSDictionary from the JSON data    
    // NSDictionary *jsonDictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];

    // Create a new array to hold the locations   
    //  NSMutableArray *locations = [[NSMutableArray alloc] init];

    // Get an array of dictionaries with the key "locations"    
    // NSArray *array = [jsonDictionary objectForKey:@"USDGBP"];

    // for (id USDGBP in array)
        //NSLog(@"VALUEEEEEE %@", array);    

    // NSLog (@"%@", [[arrayController selectedObjects] valueForKey:@"USDGBP"]);

    NSString *json_string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    NSLog(@"Data AS STRING %@", json_string);

    NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];

    double usdgbpValue = [jsonDict[@"quotes"][@"USDGBP"] doubleValue];
    //NSArray *xxx = [jsonDict[@"quotes"][@"USDGBP"] array];
    //NSLog(@"VALUEEEEEE %@", xxx);

    NSDictionary *jsonDict2 = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
    //double usdgbpValue2 = [jsonDict[@"quotes"][@"USDGBP"] doubleValue];
    NSArray *CurrencyArray = [jsonDict2 objectForKey:@"quotes"];

    NSLog(@"VALUE %@", CurrencyArray);

    //below causing crash
    //currencyArray = [[NSArray alloc]initWithArray:CurrencyArray];
    //currencyArray = CurrencyArray;
    //double usdgbpValue3 = [NSArray[@"USDGBP"]currencyArray];

    NSLog(@"USDGBP :%f", usdgbpValue);

}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 
{

    //here to display message that internet connection is needed 

}

//Creating product classes
//static Product *peas = nil;
//peas = [[Product alloc]init];

- (void)didReceiveMemoryWarning 
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated. 
}

#pragma mark - UIPickerView Datasource & Delegate Methods

// returns the number of 'columns' to display.
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

// returns the # of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return [currencyArray count]; 
}


- (nullable NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
 __TVOS_PROHIBITED
{
    return currencyArray[row];
}

/// NEW CODE 
//NSLog(@"USDGBP :%f", isSomethingEnabled);
//UILabel totalLabel.text = [NSString stringWithFormat: @"%d",
//isSomethingEnabled];

@end

当我运行代码时,我获得了grandTotal的值,但是代码显示0.000的{​​{1}}:

isSomethingEnabled

0 个答案:

没有答案