【发布时间】:2012-03-19 07:12:12
【问题描述】:
我有一些 UIPopoverViewControllers 发送消息以委托 UIViewControllers 传递 UI 事件。我没有为每个事件编写单独的方法,而是在委托中有一个带有 switch 语句的方法,该语句确定如何根据传递的常量处理事件(下面的示例)。
这可能是糟糕的设计,但这是我想出的。我见过this 有关枚举或静态类的问题,但不明白答案。
所以..是我正在做的BAD,有没有一种方法可以在一个地方定义枚举,这样我就不必维护可以轻松退出的多个代码位同步?
编辑 好的,再挖掘一下 (here + here) 我知道我可能走在正确的轨道上。所以我想我需要知道 implementation 文件在 iOS 中的位置和位置。
enum {
kSetPlaybackType = 0,
kSetAllNotesOn,
kSetAllNotesOff,
kSetVelocity,
kSetDuration
};
- (void)barPropertyAction:(int)action withParam:(NSNumber *)param
{
switch (action) {
case kSetPlaybackType:
playbackType = [param intValue];
if (playbackType == kPalindrome){
palindromeDirection = kPalindromeUp;
}
break;
case kSetAllNotesOn:
for (BarNote* note in self.barNoteArray) {
note.noteOn = YES;
}
[self.bar updateWindows];
break;
case kSetAllNotesOff:
for (BarNote* note in self.barNoteArray) {
note.noteOn = NO;
}
[self.bar updateWindows];
break;
case kSetVelocity:
for (BarNote* note in self.barNoteArray) {
note.velocity = [param intValue];
}
break;
case kSetDuration:
for (BarNote* note in self.barNoteArray) {
note.duration = [param floatValue];
}
break;
default:
break;
}
}
【问题讨论】:
标签: objective-c ios enums