我有一个NSAlert工作表,里面有一个NSComboBox。当用户按下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");
    }
}

最佳答案

在您的头文件中描述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,因为它正在分配内存。

关于objective-c - 传递NSAlert工作表中的NSComboBox值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10745076/

10-09 16:17