离开视图后保留/保留变量

时间:2012-08-17 19:06:18

标签: iphone objective-c xcode variables retain

每当我离开视图时,我很难保持/保留当前变量。因为我正在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];
}

4 个答案:

答案 0 :(得分:3)

无论你想做什么,答案都不是保留视图中被忽略的变量。

您应该将这些变量传递回您需要的地方。

您可以使用委托模式执行此操作。

https://developer.apple.com/library/mac/#documentation/General/Conceptual/DevPedia-CocoaCore/Delegation.html

或者只是在即将被解雇的视图控制器上引用另一个视图控制器。

答案 1 :(得分:2)

另一种适用于某些情境的方法:将变量存储在其他位置 - 也许您的变量是游戏状态的一个组成部分,或者它属于模型。变量可能不属于视图中的值,但在模型或游戏的状态中 - 将其存储在那里。

答案 2 :(得分:0)

你需要在当前视图中拥有一个指向该变量/属性的强指针。

答案 3 :(得分:0)

如果您通过segue离开视图,则可以将要保留的变量传递给destinationViewController。您还可以实现一个委托,该委托传递您从viewWillDisappear调用的值。

相关问题