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

Iphone 离开视图后保留/保留变量,iphone,objective-c,xcode,variables,retain,Iphone,Objective C,Xcode,Variables,Retain,无论何时离开视图,我都很难保持/保留当前变量。因为我正在Xcode 4.3上构建一个应用程序,所以ARC程序已经就位,我无法保留我的变量(关闭ARC会导致更多的问题)。有人知道即使在您离开视图后如何保留变量吗 知道我试图保留的变量是一个对象变量可能会有所帮助 编辑:这是我的代码 @synthesize dataController = _dataController; - (id)initWithStyle:(UITableViewStyle)style { self = [super

无论何时离开视图,我都很难保持/保留当前变量。因为我正在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];
}
@synthesis-dataController=\u-dataController;
-(id)initWithStyle:(UITableViewStyle)样式
{
self=[super initWithStyle:UITableViewStyleGrouped];
如果(自我){
//自定义初始化
}
回归自我;
}
-(无效)viewDidLoad
{
[超级视图下载];
//self.navigationItem.leftBarButtonItem=self.editButtonItem;
SoundDataController*aDataController=[[SoundDataController alloc]init];
self.dataController=aDataController;
//取消对下一行的注释以保留演示文稿之间的选择。
//self.clearselectiononviewwillappear=否;
//取消对以下行的注释,以在此视图控制器的导航栏中显示编辑按钮。
//self.navigationItem.rightBarButtonItem=self.editButtonItem;
}
-(无效)视图卸载
{
[超级视频下载];
//释放主视图的所有保留子视图。
//例如,self.myOutlet=nil;
}
-(布尔)应自动旋转指针面定向:(UIInterfaceOrientation)interfaceOrientation
{
返回(interfaceOrientation==UIInterfaceOrientationGraphic);
}
-(无效)AddSoundViewControllerIDCancel:(AddSoundViewController*)控制器{
[self-dismissViewControllerAnimated:是完成:空];
}
-(void)AddSoundViewControllerdFinish:(AddSoundViewController*)控制器名称:(NSString*)名称图像:(UIImage*)图像{
如果([名称长度]){
[self.dataController addSoundWithName:name image:image];
[[self tableView]重新加载数据];
}
}
#pragma标记-表视图数据源
-(NSInteger)表格视图中的节数:(UITableView*)表格视图
{
返回1;
}
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节{
返回[self.dataController countOfList];
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
静态NSString*CellIdentifier=@“SoundCell”;
UITableViewCell*单元格=[tableView
dequeueReusableCellWithIdentifier:CellIdentifier];
Sound*soundAtIndex=[self.dataController
ObjectInListIndex:indexath.row];
[[cell textlab]setText:soundAtIndex.name];
返回单元;
}
-(BOOL)tableView:(UITableView*)tableView caneditrowatinexpath:(nsindepath*)indepath
{
//如果不希望指定的项可编辑,则返回“否”。
返回YES;
}
-(iAction)切换编辑模式:(id)发送方
{
//如果我们当前处于编辑模式。。。
如果([自编辑]){
//更改按钮的文本以通知用户状态
[发件人设置标题:@“编辑”状态:UIControlStateNormal];
//关闭编辑模式
[自设置编辑:无动画:是];
}否则{
//更改按钮的文本以通知用户状态
[发送方设置标题:@“完成”状态:uicontrol状态正常];
//进入编辑模式
[自设置编辑:是动画:是];
} 
}
-(void)tableView:(UITableView*)tableView提交的编辑样式:(UITableViewCellEditingStyle)行的编辑样式索引路径:(NSIndexPath*)索引路径
{
//如果表视图请求提交删除命令。。。
如果(editingStyle==UITableViewCellEditingStyleDelete)
{
Sound*soundAtIndex=[self.dataController objectInListAtIndex:indexPath.row];
[self.dataController removeSound:soundAtIndex];
//我们还使用动画从表视图中删除该行
[tableView DeleteRowsatindExpath:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationFade];
}
else if(editingStyle==UITableViewCellEditingStyleInsert){
//[[self.dataController]位于
}
}
#pragma标记-表视图委托
-(void)tableView:(UITableView*)tableView未选择RowatineXpath:(NSIndexPath*)indexPath
{
//导航逻辑可能位于此处。创建并推送另一个视图控制器。
/*
*detailViewController=[[alloc]initWithNibName:@“bundle:nil];
// ...
//将选定对象传递给新的视图控制器。
[self.navigationController pushViewController:detailViewController动画:是];
*/
}
-(void)prepareForSegue:(UIStoryboardSegue*)segue发送方:(id)发送方
{
如果([[segue identifier]isEqualToString:@“ShowSoundDetails”]){
SoundDetailViewController*detailViewController=[segue
destinationViewController];
detailViewController.sound=[self.dataController
ObjectInListIndex:[self.tableView indexPathForSelectedRow].row];
}
else if([[segue identifier]IsequalString:@“ShowAddSoundView”]){
AddSoundViewController*addController=
(AddSoundViewController*)[[[segue destinationViewController]
视图控制器]对象索引:0];
addController.delegate=self;
}
//[自我解散Modalviewcontrolleranimated:是];
}

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

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

无论您选择什么要做到这一点,答案是不要在被忽略的视图中保留变量

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

YouC