每当我离开视图时,我都很难保持/保留我当前的变量。因为我在 Xcode 4.3 上构建应用程序,所以 ARC 程序就位,我无法保留我的变量(并且关闭 ARC 导致的问题多于其价值)。即使在您离开视图后,有人知道如何保留变量吗?
知道我要保留的变量是对象变量可能会有所帮助。
编辑:这是我的代码。
@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];
}