保存操作表值/用户默认值

时间:2015-01-01 12:04:27

标签: ios uitableview nsuserdefaults uiactionsheet

所以在我的应用程序中,我使用了一个动作表来显示要使用的动画。一切都已实现,但是,我在使用用户默认值保存所选行时遇到问题,然后将其传递给我的其余代码。我在UISwitch之前使用过用户默认设置,但我想这是不同的。

我使用的部分是UITableView。在didselectrow

                //Animation Picker
                [tableView deselectRowAtIndexPath:indexPath animated:NO];

                NSString *actionSheetTitle = @"Choose your style.";

                BlockActionSheet *sheet = [BlockActionSheet sheetWithTitle:actionSheetTitle];

                UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
                NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];


                [sheet addButtonWithTitle:@"Zoom Out" block:^{
                    [cell.textLabel setText:[NSString stringWithFormat:@"Animation Style: Zoom Out"]];
                    //User Defaults
                    [prefs setObject:@"Zoom Out" forKey:@"Zoom"];

                    cellTitle = @"Zoom Out";
                }];

                [sheet addButtonWithTitle:@"Drop In" block:^{
                    [cell.textLabel setText:[NSString stringWithFormat:@"Animation Style: Drop In"]];
                    //User Defaults
                    [prefs setObject:@"Drop In" forKey:@"Drop"];

                    cellTitle = @"Drop In";
                }];

                [sheet setDestructiveButtonWithTitle:@"Cancel" block:nil];
                [sheet showInView:self.view];

                [prefs synchronize];
                [tableView reloadData];

哪个应该发送信息以使用正确的animationController

-(void)infoButtonAction:(id)sender {

    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults synchronize];

    if ([userDefaults stringForKey:@"Zoom"]) {

        AppInfoViewController * info = [[AppInfoViewController alloc] init];
        UINavigationController *vc = [[UINavigationController alloc] initWithRootViewController:info];
        self.animationController = [[ZoomAnimationController alloc] init];
        vc.transitioningDelegate  = self;
        [self presentViewController:vc animated:YES completion:nil];
        [info release];
    }

    else if ([userDefaults stringForKey:@"Drop"]) {

        AppInfoViewController * info = [[AppInfoViewController alloc] init];
        UINavigationController *vc = [[UINavigationController alloc] initWithRootViewController:info];
        self.animationController = [[DropAnimationController alloc] init];
        vc.transitioningDelegate  = self;
        [self presentViewController:vc animated:YES completion:nil];
        [info release];
    }

    NSLog(@"App Info");
}

还应该更新显示选择了animationController的单元格:

                NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
                [prefs synchronize];
                [cell.textLabel setText:[NSString stringWithFormat:@"Animation Style:"]];

                if ([prefs stringForKey:@"Zoom"]){
                    [cell.textLabel setText:[NSString stringWithFormat:@"Animation Style: Zoom Out"]];
                }

                else if ([prefs stringForKey:@"Drop"]) {
                    [cell.textLabel setText:[NSString stringWithFormat:@"Animation Style: Drop In"]];
                }

                UIView *animation = [[UIView alloc] initWithFrame:cell.frame];
                animation.backgroundColor = [UIColor colorWithRed:0.176 green:0.176 blue:0.176 alpha:1];
                cell.backgroundView = animation;

然而,没有任何东西得救。我错过了什么?

感谢您的帮助。

更新:1/3 我设法使用上面的更新代码。现在唯一的事情是它不会挽救我选择的风格。如果我选择Drop In,然后重新打开表格,它将恢复为缩小。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

取一个

的String对象
    NSString *OneString = [NSString stringWithFormat: selectedValue];

并在if条件和其余代码中使用OneString。