gpt4 book ai didi

ios - 无法将 UISegmentedControl 与核心数据混合

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:07:06 26 4
gpt4 key购买 nike

我一直在寻找一些弱点的答案,但我似乎找不到。我一直在尝试获取 UISegmentedControl 的数据以保存在核心数据中,但我做不到,它一直向我显示错误和警告,希望你能帮助我。

我有这样的东西:

#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。我该怎么办?

最佳答案

[self.teamData valueForKey:@"considered"]

这可能会返回一个 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];
}

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

关于ios - 无法将 UISegmentedControl 与核心数据混合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22272567/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com