2

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

4 回答 4

3

无论您尝试做什么,答案都不是在被忽略的视图中保留变量。

您应该将这些变量传递回您需要它们的位置。

您可以使用委托模式来做到这一点。

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

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

于 2012-08-17T19:17:26.867 回答
2

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

于 2012-08-17T20:13:20.487 回答
0

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

于 2012-08-17T19:15:33.563 回答
0

您需要在当前视图中包含指向该变量/属性的强指针。

于 2012-08-17T19:15:20.087 回答