0

before the closing of my FlipsideViewController I have these values

shours
sminutes
sseconds
srecharge
change

from the declaration

int shours;
int sminutes;
int sseconds;
int srecharge;
bool change;

Now I want to pass these variables to another UIViewController (MainViewController) in these other variables

mhours
mminutes
mseconds
mrecharge
mchange

What's the simplest method to do this?

5

3 Answers 3

0

Create a custom object property on your MainViewController and set it on your FlipsideViewController or you can't do it for some reason: Create an object to hold these values and put it into NSUserDefaults and read on MainViewController.

3
  • [[NSUserDefaults standardUserDefaults] setObject:myObject forKey:@"myObjectKey"]
    – Dave
    Nov 15, 2012 at 15:06
  • I want to pass int values and a bool, not NSString Nov 15, 2012 at 15:15
  • As I've mentioned: you should create an object to hold the ints and bools. Or you can store the one by one in an NSNumber ([NSNumber numberwithInt:/numberWithBool:]
    – Dave
    Nov 15, 2012 at 15:27
0

I would simply create a "Model" class that holds those vars and pass the model class from one view controller to the next. Once it's linked in view1 & view2 during transition from view1 to view2, then it's automatically updated when transitioning back from view2 to view1. Use @property (assign) int shours so you have setters and getters created.

With this approach, using segues will work just fine. You don't need to use NSUserDefaults or NSNotificationCenter.

0

I would create a class to hold these values. Then pass this object between view controllers.

Something like this:

Interface file:

//
//  MyObject.h
//  SOObjectPassing
//
//  Created by Wilson, LJ on 11/15/12.
//  Copyright (c) 2012 Arkansas Children's Hospital. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface MyObject : NSObject {
    int shours;
    int sminutes;
    int sseconds;
    int srecharge;
    bool change;
}

@property (nonatomic, assign) int shours;
@property (nonatomic, assign) int sminutes;
@property (nonatomic, assign) int sseconds;
@property (nonatomic, assign) int srecharge;
@property (nonatomic, assign) BOOL change;

-(id) initWithHours:(int)hours
            minutes:(int)minutes
            seconds:(int)seconds
           recharge:(int)recharge
             changed:(BOOL)changed;

@end

Implementation file:

//
//  MyObject.m
//  SOObjectPassing
//
//  Created by Wilson, LJ on 11/15/12.
//  Copyright (c) 2012 Arkansas Children's Hospital. All rights reserved.
//

#import "MyObject.h"


@implementation MyObject
@synthesize shours = _shours;
@synthesize sminutes = _sminutes;
@synthesize sseconds = _sseconds;
@synthesize srecharge = _srecharge;
@synthesize change = _change;

-(id) initWithHours:(int)hours
            minutes:(int)minutes
            seconds:(int)seconds
           recharge:(int)recharge
             changed:(BOOL)changed {

    if ((self = [super init])) {
        _shours = hours;
        _sminutes = minutes;
        _sseconds = seconds;
        _srecharge = recharge;
        _change = changed;
    }

    return self;
}
@end

And instantiate your object like this:

MyObject *myObject = [[MyObject alloc] initWithHours:2
                                                 minutes:3
                                                 seconds:4
                                                recharge:1
                                                 changed:NO];

Then just pass that entire object to your other VC (s).

Here is a sample project illustrating this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.