是否可以在 Objective-C 中声明诸如 Delegates 之类的匿名实现。我认为我的术语是正确的,但这是一个 java 示例:
myClass.addListener(new FancyInterfaceListener({
void onListenerInterestingAction(Action a){
....interesting stuff here
}
});
因此,例如要处理 UIActionSheet 调用,我必须在同一个类中声明另一个方法,如果我想向它传递数据,这似乎有点愚蠢,因为我必须将该数据存储为全局变量。这是一个使用确认对话框删除某些内容的示例,询问您是否确定:
-(void)deleteItem:(int)indexToDelete{
UIActionSheet *confirm = [[UIActionSheet alloc] initWithTitle:@"Delete Item?" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:@"Delete" otherButtonTitles:nil];
[confirm showInView:self.view];
[confirm release];
}
和同一类中的 UIActionSheetDelegate:
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex == 0){
[[Settings sharedSettings] removeItemAtIndex:/*need index variable here*/];
[drinksTable reloadData];
}
}
我想要做的是内联声明它,就像我在顶部的 java 示例中所做的那样。这可能吗?