2017-11-09 4 views
0

MacOS의 Swift 4에 NSAlert를 사용하여 로그인 대화 상자를 구성하고 있습니다.NSAlert에 NSTextField가 여러 개 있습니다 - MacOS

func dialogOKCancel(question: String, text: String) -> (String, String) { 
     let alert = NSAlert() 
     alert.messageText = question 
     alert.informativeText = text 
     alert.alertStyle = .warning 

     alert.addButton(withTitle: "Login") 
     alert.addButton(withTitle: "Cancel") 


     let unameField = NSTextField(frame: NSRect(x: 0, y: 0, width: 200, height: 24)) 
     let passField = NSSecureTextField(frame: NSRect(x: 0, y: 0, width: 200, height: 24)) 

     let stackViewer = NSStackView() 
     stackViewer.addSubview(unameField) 

     alert.accessoryView = stackViewer 

     let response: NSApplication.ModalResponse = alert.runModal() 

     if (response == NSApplication.ModalResponse.alertFirstButtonReturn) { 
      return (unameField.stringValue, passField.stringValue) 
     } else { 
      return ("", "") 
     } 
    } 

이 중 하나 unameField 또는 passField 내가 alert.accessoryView = passField을 수행 할 때 보여주기 위해 잘 작동합니다 : 여기에 지금까지 가지고있는 코드입니다. 하지만 두 필드를 같은 대화 상자에 표시하려고합니다. 보시다시피 NSStackView (및 기타) 실험했지만 두 가지 요소를 모두 보여주는 솔루션을 찾지 못했습니다.

답변

2
희망이 당신을 도울 것입니다

...

당신은 NSStackView 및 addSubview이 항목을 사용할 수 있습니다 unameField 및 passField을. 하지만 NSStackView에 프레임을 설정하고 NSStackView에 2 개의 항목을 설정해야합니다.

let unameField = NSTextField(frame: NSRect(x: 0, y: 2, width: 200, height: 24)) 

let passField = NSSecureTextField(frame: NSRect(x: 0, y: 28, width: 200, height: 24)) 

let stackViewer = NSStackView(frame: NSRect(x: 0, y: 0, width: 200, height: 58)) 

stackViewer.addSubview(unameField) 

stackViewer.addSubview(passField) 

alert.accessoryView = stackViewer 

을 그리고 이것은 내 결과입니다 :

이 내 코드, 당신은 참조 할 수 있습니다

my result

+0

아, 물론 그것이 자신의 NSRect이 필요 해요! 정말 고맙습니다! – tornato7

관련 문제