我有一张内部有NSComboBox的NSAlert Sheet。当用户按下NSAlert的按钮时,如何传递组合框值?
代码:
NSComboBox* comboBox = [[NSComboBox alloc] initWithFrame:NSMakeRect(0, 0, 150, 26)];
[comboBox setTitleWithMnemonic:@"2"];
for (int i=2; i<[array count]+1; i++){
[comboBox addItemWithObjectValue:[NSString stringWithFormat:@"%i", i]];
}
[comboBox setEditable:NO];
NSAlert *alert = [[NSAlert alloc] init];
[alert addButtonWithTitle:@"Okay"];
[alert addButtonWithTitle:@"Cancel"];
[alert setMessageText:@"Choose a number"];
[alert setAccessoryView:comboBox];
[alert beginSheetModalForWindow:_window modalDelegate:self didEndSelector:@selector(alertToChooseX:returnCode:contextInfo:) contextInfo:nil];
- (void)alertToChooseX:(NSAlert *)alert returnCode:(NSInteger)returnCode contextInfo:(void *)contextInfo {
if (returnCode == NSAlertFirstButtonReturn) {
NSLog(@"Pressed Okay");
}
}
答案 0 :(得分:0)
在您的标题文件中描述 comboBox ,并在按下“ Okay ”后按下它的值:
.h 中的
NSComboBox *comboBox;
.m 中的
comboBox = [[NSComboBox alloc] initWithFrame:NSMakeRect(0, 0, 150, 26)];
[comboBox setTitleWithMnemonic:@"2"];
.... // Your all code goes here
- (void)alertToChooseX:(NSAlert *)alert returnCode:(NSInteger)returnCode contextInfo:(void *)contextInfo {
if (returnCode == NSAlertFirstButtonReturn) {
NSLog(@"Pressed Okay");
NSLog(@"Selected ComboBox's String Value: %@", [comboBox stringValue]);
NSLog(@"Selected ComboBox's Object Value: %@", [comboBox objectValueOfSelectedItem]);
NSLog(@"Selected ComboBox's Item Index: %ld", [comboBox indexOfSelectedItem]);
}
}
注意:不要忘记发布 comboBox ,因为它正在分配内存。