带核心数据的分段控制器(多选)

发布于 2024-08-17 17:38:35 字数 364 浏览 7 评论 0 原文

我有一个基于核心数据的应用程序,它是围绕一个主要实体构建的。还有其他几个实体与其连接,其中之一是名为“Notes”的实体。

此 Notes 实体具有一个日期 (NSDate)、一个描述 (NSString) 和一个其他属性。这个属性有 4 个可能的选项,其中每个实体至少有 1 个,也可能有全部 4 个。

我在想,当创建一个 Note 时,可能会有一个带有 4 个可能选项的分段控制器。 (是否可以在此处选择多个按钮?)

我还希望能够通过此选项对所有这些笔记进行排序。也就是说,创建一个仅返回选择了选项 3 的注释的提取请求。 (即使他们也选择了选项 2,甚至选择了所有 4 个选项。)

关于实现这一点的最佳方法有什么建议吗?

谢谢。

I have a Core Data based application which is built around one main entity. There are several other entities which are connected to it, one of which is an Entity called "Notes".

This Notes entity has a Date (NSDate), a Description (NSString), and one other attribute. This attribute is to have 4 possible options, of which each entity will have at least 1 and possibly all 4.

I was thinking that when a Note is being created, there could be a Segmented Controller with the 4 possible options. (Is it even possible to select multiple buttons here?)

I further want to be able to sort all of these notes by this option. That is, to create a fetch request that returns only the Notes that had Option 3 selected, for example. (Even if they also had Option 2, or even all 4 options selected.)

Any suggestions on what the best way to implement this is?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

日记撕了你也走了 2024-08-24 17:38:35

要使用掩码来存储多个选择,您可以执行如下操作:

NSInteger const kNoteOptionTypeOne = 0x1;   // 0000 0001
NSInteger const kNoteOptionTypeTwo = 0x2;   // 0000 0010
NSInteger const kNoteOptionTypeThree = 0x4; // 0000 0100
NSInteger const kNoteOptionTypeFour = 0x8;  // 0000 1000

对于多个选择,您仍然会将组合掩码存储为 NSNumber,例如,

NSInteger mySelectionsMask = (kNoteOptionTypeOne | kNoteOptionTypeFour); // 0x0 | 0x4 = 1001 = 9
NSNumber *mySelections = [NSNumber numberWithInt:mySelectionsMask];

mySelections 值将对于四个选项的某种组合来说是唯一的。您可以从这个组合掩码返回到单独的掩码,以便选择分段控件的不同按钮,例如:

if ([mySelections intValue] == (kNoteOptionTypeOne | kNoteOptionTypeFour)) {
    // select buttons one and four of the segmented control
}
else if some other combination { etc. }

或者:

if (([mySelections intValue] & kNoteOptionTypeOne) == kNoteOptionTypeOne) 
    // select button one
if (([mySelections intValue] & kNoteOptionTypeTwo) == kNoteOptionTypeTwo)
    // select button two
... 

由于它存储为 NSNumber,因此您将能够对其进行排序具有如上所述的 NSSortDescriptor 实例。

To use masks for storing multiple selections, you might do something like the following:

NSInteger const kNoteOptionTypeOne = 0x1;   // 0000 0001
NSInteger const kNoteOptionTypeTwo = 0x2;   // 0000 0010
NSInteger const kNoteOptionTypeThree = 0x4; // 0000 0100
NSInteger const kNoteOptionTypeFour = 0x8;  // 0000 1000

For multiple selections, you would still store your combined mask as an NSNumber, e.g.

NSInteger mySelectionsMask = (kNoteOptionTypeOne | kNoteOptionTypeFour); // 0x0 | 0x4 = 1001 = 9
NSNumber *mySelections = [NSNumber numberWithInt:mySelectionsMask];

This mySelections value will be unique to some combination of the four options. You can go back from this combined mask to individual masks, in order to select different buttons of a segmented control, e.g.:

if ([mySelections intValue] == (kNoteOptionTypeOne | kNoteOptionTypeFour)) {
    // select buttons one and four of the segmented control
}
else if some other combination { etc. }

Or:

if (([mySelections intValue] & kNoteOptionTypeOne) == kNoteOptionTypeOne) 
    // select button one
if (([mySelections intValue] & kNoteOptionTypeTwo) == kNoteOptionTypeTwo)
    // select button two
... 

As this is stored as an NSNumber, you will be able to sort on it with an NSSortDescriptor instance as described above.

玩套路吗 2024-08-24 17:38:35

首先,我不会尝试使用一个属性来收集四个可能的选项,而是使用四个单独的布尔属性。这也将允许您非常轻松地过滤您的获取请求。

要配置每个布尔值,我将使用 UIButton 或 UISwitch。 UISegmentedControl 不支持多项选择。

First, instead of trying to use one attribute to collect four possible options, I would use four separate boolean attributes. This will also allow you to filter your fetch requests very easily.

To configure each boolean, I would use a UIButton or UISwitch. The UISegmentedControl does not support multiple selection.

沫尐诺 2024-08-24 17:38:35

使用枚举来定义四个选项,例如:

typedef enum _NoteOptionType {
    kNoteOptionTypeOne,
    kNoteOptionTypeTwo,
    kNoteOptionTypeThree,
    kNoteOptionTypeFour,
    kNoteOptionTypes,
} NoteOptionType;

这些选项的编号为 0 到 5。Core

Data 将整数存储为 NSNumber 实例。您也许可以在 Note 实体中保留一个名为 optionType 的属性,它存储与 NoteOptionType 等效的 NSNumber > 价值。

您可以通过诸如 [NSNumber numberWithInt:kNoteOptionTypeOne] 之类的方式将这些选项转换为 NSNumber 选项。

您可以编写一个方便的方法来将 NoteOptionType 转换为字符串以放入 UISegmentedControl 中,例如:

+ (NSString *) keyForNoteOptionTypeTag:(NoteOptionType)optionTypeTag {
    if (optionTypeTag == kNoteOptionTypeOne)
        return [NSString stringWithFormat:@"First"];     
    else if (optionTypeTag == kNoteOptionTypeTwo)
        return [NSString stringWithFormat:@"Second"];
    ...
    return [NSString stringWithFormat:@"Undefined"];
}

像这样引用它:

NSLog(@"second option is: %@", [Note keyForNoteOptionTypeTag:kNoteOptionTypeTwo]);

在您的获取中,您可以使用 NSSortDescriptor 将 >NSNumber 值放入核心数据存储中作为排序条件,例如:

NSSortDescriptor *optionTypeDescriptor = [[NSSortDescriptor alloc] initWithKey:@"optionType" ascending:YES selector:nil];
NSArray *sortDescriptors = [NSArray arrayWithObjects:optionTypeDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
[optionTypeDescriptor release];

Use an enumeration to define four options, e.g.:

typedef enum _NoteOptionType {
    kNoteOptionTypeOne,
    kNoteOptionTypeTwo,
    kNoteOptionTypeThree,
    kNoteOptionTypeFour,
    kNoteOptionTypes,
} NoteOptionType;

These will be numbered 0 through 5.

Core Data stores integers as NSNumber instances. You could perhaps keep an attribute in your Note entity that is called optionType, which stores the NSNumber equivalent of a NoteOptionType value.

You can convert these to NSNumber options through something like, for example, [NSNumber numberWithInt:kNoteOptionTypeOne].

You could write a convenience method to convert a NoteOptionType to a string to put into a UISegmentedControl, e.g.:

+ (NSString *) keyForNoteOptionTypeTag:(NoteOptionType)optionTypeTag {
    if (optionTypeTag == kNoteOptionTypeOne)
        return [NSString stringWithFormat:@"First"];     
    else if (optionTypeTag == kNoteOptionTypeTwo)
        return [NSString stringWithFormat:@"Second"];
    ...
    return [NSString stringWithFormat:@"Undefined"];
}

Reference it like so:

NSLog(@"second option is: %@", [Note keyForNoteOptionTypeTag:kNoteOptionTypeTwo]);

In your fetch, you can use the NSNumber values you put in your Core Data store as criteria on which to sort, through the use of an NSSortDescriptor, e.g.:

NSSortDescriptor *optionTypeDescriptor = [[NSSortDescriptor alloc] initWithKey:@"optionType" ascending:YES selector:nil];
NSArray *sortDescriptors = [NSArray arrayWithObjects:optionTypeDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
[optionTypeDescriptor release];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文