Pass an int between a view and it's subview
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 = [[SolutionsVie开发者_高级运维wController alloc] init];
solutions.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:solutions animated:YES];
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;
}
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);
}
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
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];
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
精彩评论