简体   繁体   中英

Objective-C: Passing double between viewControllers?

If I have viewController1 and viewController2 and user enters a double value into a textField in viewController1 this is passed to a variable in viewController2 and that value can be used.

This variable must then be able to be transferred to the UIView as that is what requires the value.

I'm struggling even at the first hurdle and that is to make a double a @property or what it needs to be, A lot of tutorials and examples show NSStrings being transferred but it is important that I transfer a double.

Here's what I have so far..

ViewControllerInputs.h - viewcontroller1

@interface ViewControllerInputs : UIViewController {
    IBOutlet UITextField *thicknessField;
    IBOutlet UITextField *capWidthField;

    double *thicknessValue;
    double *capWidthValue;
}

ViewControllerImage.h - viewcontroller2

@interface ViewControllerImage : UIViewController {
        double thicknessValue1;
        double capWidthValue1;
}

ViewControllerInputs.m - viewcontroller1

-(IBAction)createWeld {



    ViewControllerImage *secondViewController = [[ViewControllerImage alloc]
    initWithNibName:@"ViewControllerImage" bundle:nil];
    ViewControllerImage.thicknessValue1 = thicknessValue;
    //Require modal/push to viewControllerImage
}

UIView inside ViewControllerImage - viewcontroller2

This is where the variables thicknessValue1 and CapWidthValue1 will be used.

You have a few problems:

1) Get rid of the asterisk for each of the double ivars

2) Get rid of the double ivars and use properties instead.

@property (nonatomic, assign) double thicknessValue1;
@property (nonatomic, assign) double capWidthField;

As you have it, you are attempting to assign to a non-existent property.

Use

@property (nonatomic) double someDoubleVariable; for example to define the property, in the interface after defining the instance variable,

@synthesize someDoubleVariable; within the implementation to generate the getter and setter functions.

I suggest you read the documentation on Objective-C 2.0 properties — you're lucky to be programming for a platform whose parent company makes the cleanest documentation. Make good use of it.

Good luck.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM