1

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];
WFT
  • 267
  • 3
  • 11

5 Answers5

1
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;
}
Alex Wayne
  • 178,991
  • 47
  • 309
  • 337
  • 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 Jul 27 '10 at 02:21
  • I figured it out, with just a few minor adjustments to the code you gave me. Thanks so much – WFT Jul 27 '10 at 20:17
1

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);
}
WFT
  • 267
  • 3
  • 11
0

The selected answer works for me but it is giving me a semantic warning. I'm a little annal retentive about warnings even if the code works so I am wondering if there is a way to make it work without the warning.

The warning is:

Instance method '-SetPrompt:' not found (return type defaults to 'id')

Here is what I did while following along to the answer in this question.

In the calling .m file

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    vcSelection *ViewSelection = [[vcSelection alloc] initWithNibName:@"vcSelection" bundle:nil];
    ViewSelection.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    if ([[SettingsTableData objectAtIndex:indexPath.row] isEqualToString:DeviceType])
    {
        [ViewSelection SetPrompt:@"Select the device type."];
    }
    else if ([[SettingsTableData objectAtIndex:indexPath.row] isEqualToString:DeviceManufacturer])
    {
        [ViewSelection SetPrompt:@"Select the device manufacturer."];
    }
    else if ([[SettingsTableData objectAtIndex:indexPath.row] isEqualToString:DeviceModel])
    {
        [ViewSelection SetPrompt:@"Select the device model."];
    }
    [self.view addSubview:ViewSelection.view];
}

In the receiving .m file

@implementation vcSelection

NSMutableString *Prompt;

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Debug"
                                                    message:Prompt
                                                   delegate:self
                                          cancelButtonTitle:@"Done"
                                          otherButtonTitles:nil];
    [alert show];
    [alert release];
}

- (void) SetPrompt:(NSMutableString *)Value
{
    Prompt = Value;
}

@end
Daisy
  • 121
  • 11
0

I would use a Singleton class.

What should my Objective-C singleton look like?

Then you can do like:

[SingletonClass sharedInstance].var = iteration;

And access it with:

[SingletonClass sharedInstance].var
Community
  • 1
  • 1
Kurbz
  • 11,003
  • 2
  • 17
  • 12
  • 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 Jul 26 '10 at 23:30
  • Yea that is a lot simpler, I've never really thought about doing it that way. – Kurbz Jul 26 '10 at 23:37
0

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
  • 5,018
  • 27
  • 22