views:

67

answers:

4

I have a view called PatternsViewController and a subview named SolutionsViewController. I want to pass a variable in PatternsViewController named iteration to my SolutionsViewController, right before I present it with

solutions = [[SolutionsViewController alloc] init];
solutions.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
 [self presentModalViewController:solutions animated:YES];

thanks in advance,
wft

+1  A: 
solutions = [[SolutionsViewController alloc] init];
solutions.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;

// Set your value here
[solutions setMyIntWithSomeMethodIWrote:123];

[self presentModalViewController:solutions animated:YES];

And in SolutionsViewController

- (void)setMyIntWithSomeMethodIWrote:(int)value {
    myInstanceVar = value;
}
Squeegy
This looks really simple, I'd love it if it were this easy but I've got an error with my method in SolutionsViewController. The compiler expects a '(' before 'value' and an identifier before '{'. I'm kind of new to this and I don't know where to put the '('.
WFT
I figured it out, with just a few minor adjustments to the code you gave me. Thanks so much
WFT
Hah, sorry. I've been writing Java lately and had my syntax confused...
Squeegy
A: 

I would use a Singleton class.

http://stackoverflow.com/questions/145154/what-does-your-objective-c-singleton-look-like

Then you can do like:

[SingletonClass sharedInstance].var = iteration;

And access it with:

[SingletonClass sharedInstance].var
Kurbz
All that work setting up a Singleton for a single variable... might want to take a look at @squeegy's answer and also the last question you suggested this for http://stackoverflow.com/questions/3325159/how-to-pass-text-between-views/3325992#3325992. Just saying this because there is a simpler way.
iWasRobbed
Yea that is a lot simpler, I've never really thought about doing it that way.
Kurbz
A: 

I figured it out by slightly modifying Squeegy's code.

In PatternsViewController.m

[solutions setIteration:iteration];

and in SolutionsViewController.m

    -(void) setIteration:(int)value {
 iteration = value;
 NSLog(@"Iteration set from value: %d" , iteration);
}

Thanks again,
Squeegy

WFT
A: 

Why not simply over-ride the init with an additional argument that take the int you want to set? This allows a clean instantiation without an added set call.

solutions = [[SolutionsViewController alloc] initWithIntVal: int];
MystikSpiral