ビューを離れるたびに、現在の変数を保持/保持するのに苦労しています。私は 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];
}