无法将UISegmentedControl与Core Data混合使用

时间:2014-03-08 17:27:13

标签: ios database core-data uisegmentedcontrol

我一直在寻找几个弱点的答案,但我似乎无法找到它。我一直试图获取UISegmentedControl的数据以保存在Core Data中,但是我不能这样做,它一直向我显示错误和警告,希望你能帮助我。

我有这样的事情:

#import "DetailScoutingViewController.h"

@interface DetailScoutingViewController ()

    @property (strong, nonatomic) IBOutlet UITextField *name;
    @property (strong, nonatomic) IBOutlet UITextField *number;
    @property (strong, nonatomic) IBOutlet UISegmentedControl *considered;

    - (IBAction)save:(id)sender;
    - (IBAction)cancel:(id)sender;

    @property (strong, nonatomic) NSManagedObject *teamData;

@end

@implementation DetailScoutingViewController
@synthesize teamData;

- (void)viewDidLoad
{
    [super viewDidLoad];

    if (self.teamData) {

        [self.name setText:[self.teamData valueForKey:@"name"]];
        [self.number setText:[self.teamData valueForKey:@"number"]];
        [self.considered setSelectedSegmentIndex:[self.teamData valueForKey:@"considered"]];
    }
}

- (NSManagedObjectContext *)managedObjectContext {
    NSManagedObjectContext *context = nil;
    id delegate = [[UIApplication sharedApplication] delegate];
    if ([delegate performSelector:@selector(managedObjectContext)]) {
        context = [delegate managedObjectContext];
    }
    return context;
}

- (IBAction)save:(id)sender {

    NSManagedObjectContext *context = [self managedObjectContext];

    if (self.teamData) {
        // Update existing device
        [self.teamData setValue:self.name.text forKey:@"name"];
        [self.teamData setValue:self.number.text forKey:@"number"];
        [self.teamData setValue:self.considered.selectedSegmentIndex forKey:@"considered"];

    } else {
        // Create a new device
        NSManagedObject *newDevice = [NSEntityDescription insertNewObjectForEntityForName:@"Teams" inManagedObjectContext:context];
        [newDevice setValue:self.name.text forKey:@"name"];
        [newDevice setValue:self.number.text forKey:@"number"];
    }

    NSError *error = nil;
    // Save the object to persistent store
    if (![context save:&error]) {
        NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]);
    }

    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)cancel:(id)sender {

    [self.navigationController popViewControllerAnimated:YES];
}

UITextField的数据保存没有问题,我唯一的问题是UISegmentedControl。我该怎么办?

1 个答案:

答案 0 :(得分:2)

  

[self.teamData valueForKey:@“考虑”]

这会返回可能是NSNumber实例的内容,但setSelectedSegmentIndex:需要一个NSInteger,因此您应该使用:

[self.considered setSelectedSegmentIndex:[[self.teamData valueForKey:@"considered"] integerValue]];

您还需要将相应的保存代码更改为:

[self.teamData setValue:[NSNumber numberWithInteger:self.considered.selectedSegmentIndex] forKey:@"considered"];

这段代码没有帮助:

if ([delegate performSelector:@selector(managedObjectContext)]) {
    context = [delegate managedObjectContext];
}

更改为:

if ([delegate respondsToSelector:@selector(managedObjectContext)]) {
    context = [delegate managedObjectContext];
}

在某些其他控制器的某个时刻,您应该设置...teamData = ...。如果不是,那么您的控制器将始终创建一个新的Teams托管对象并将其插入数据存储中。在这种情况下,您不设置self.considered.selectedSegmentIndex,因此您永远不会存储它。只有在您拥有Teams对象时才会存储它。