2012-05-24 2 views
1

NSComboBox가있는 NSAlert 시트가 있습니다. 사용자가 NSAlert 버튼을 누르면 콤보 상자 값을 전달할 수 있습니까?
코드 :NSAlert 시트 내에있는 NSComboBox 값 전달

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

귀하의 헤더 파일에 으로 comboBox을 설명하고 "좋아"이 같은 값이다 걸릴 버튼을 누르지 후 :

.H

NSComboBox *comboBox; 

에서 in .m


- (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 = [[NSComboBox alloc] initWithFrame:NSMakeRect(0, 0, 150, 26)]; [comboBox setTitleWithMnemonic:@"2"]; .... // Your all code goes here 

6,:는 메모리를 할당 있기 때문에 으로 comboBox에게를 해제하는 것을 잊지 마십시오.

+0

그런 식으로 생각하고 있었지만 NSAlert에 값을 전달할 방법이 있다고 생각했습니다. 고맙습니다! 그리고 btw, 나는 ARC를 사용하지 않으므로 릴리즈가 : P –

+0

@PedroVieira 당신은 환영합니다 :) –