Reputation: 45
I am having a hard time keeping/retaining my current variables whenever I leave a view. Because I am building an application on Xcode 4.3, the ARC program is in place and I cannot retain my variables (and turning off the ARC causes more problems than it's worth). Does anybody know how to retain variables even after you leave the view?
It might help to know that the variable I'm trying to retain is an object variable.
Edit: Here's my code.
@synthesize dataController = _dataController;
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:UITableViewStyleGrouped];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// self.navigationItem.leftBarButtonItem = self.editButtonItem;
SoundDataController *aDataController = [[SoundDataController alloc] init];
self.dataController = aDataController;
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (void)addSoundViewControllerDidCancel:(AddSoundViewController *)controller {
[self dismissViewControllerAnimated:YES completion:NULL];
}
- (void)addSoundViewControllerDidFinish:(AddSoundViewController *)controller name:(NSString *)name image:(UIImage *)image {
if ([name length]) {
[self.dataController addSoundWithName:name image:image];
[[self tableView] reloadData];
}
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.dataController countOfList];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"SoundCell";
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:CellIdentifier];
Sound *soundAtIndex = [self.dataController
objectInListAtIndex:indexPath.row];
[[cell textLabel] setText:soundAtIndex.name];
return cell;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}
-(IBAction)toggleEditingMode:(id)sender
{
// If we are currently in editing mode...
if ([self isEditing]) {
// Change text of button to inform user of state
[sender setTitle:@"Edit" forState:UIControlStateNormal];
// Turn off editing mode
[self setEditing:NO animated:YES];
} else {
// Change text of button to inform user of state
[sender setTitle:@"Done" forState:UIControlStateNormal];
// Enter editing mode
[self setEditing:YES animated:YES];
}
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
// If the table view is asking to commit a delete command...
if (editingStyle == UITableViewCellEditingStyleDelete)
{
Sound *soundAtIndex = [self.dataController objectInListAtIndex:indexPath.row];
[self.dataController removeSound:soundAtIndex];
// We also remove that row from the table view with an animation
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationFade];
}
else if (editingStyle == UITableViewCellEditingStyleInsert) {
// [[self.dataController] in
}
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Navigation logic may go here. Create and push another view controller.
/*
<#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
// ...
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:detailViewController animated:YES];
*/
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"ShowSoundDetails"]) {
SoundDetailViewController *detailViewController = [segue
destinationViewController];
detailViewController.sound = [self.dataController
objectInListAtIndex:[self.tableView indexPathForSelectedRow].row];
}
else if ([[segue identifier] isEqualToString:@"ShowAddSoundView"]) {
AddSoundViewController *addController =
(AddSoundViewController *)[[[segue destinationViewController]
viewControllers] objectAtIndex:0];
addController.delegate = self;
}
// [self dismissModalViewControllerAnimated:YES];
}
Upvotes: 2
Views: 2538
Reputation: 104698
Another approach, applicable in some contexts: Store the variable someplace else -- perhaps your variable is a component of a game's state, or it belongs in a model. The variable may not belong as a value in a view, but in the model or game's state -- store it there.
Upvotes: 2
Reputation: 1109
Whatever you're trying to do, the answer is not to retain the variables in the view that is being dismissed.
You should pass those variables back to where you need them.
You can do this by using the delegate pattern.
or just have a reference to the other view controller, on the view controller that is about to be dismissed.
Upvotes: 3
Reputation: 1801
If you leave the view via segue, you could pass the variable you want to keep to the destinationViewController. You could also implement a delegate that is passed your value that you call from viewWillDisappear.
Upvotes: 0
Reputation: 5112
You need to have something in the current view holding a strong pointer to that variable/property.
Upvotes: 0