gpt4 book ai didi

objective-c - 传递 NSAlert Sheet 内的 NSComboBox 值

转载 作者:行者123 更新时间:2023-12-03 17:24:30 25 4
gpt4 key购买 nike

我有一个 NSAlert Sheet,里面有一个 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,并在按下按钮“好的”后获取如下值:

.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 Sheet 内的 NSComboBox 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10745076/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com