Jack44 Jack44 - 1 year ago 131
Swift Question

Alert with password input in Swift OS X

I am trying to create a OS X app using Swift, and it's been going well until now!

What I want to do is prompt user to enter a password in an alert (or any dialog really), save it in a variable and use it later on. Now, this app is just to get my hands dirty, so I don't even want the input box to be secure. It just needs to popup and ask for text input.

I have looked everywhere. Scanned the

NSAlert
reference, there seems to be no easy way of doing this. In contrast, on iOS
UIAlertController
does this very easily.

What I tried to do is create an
NSAlert
instance and modify its
AccessoryView
as suggested in NSAlert Class Reference, but that does not let me add an
NSTextField
.

Is there something I'm missing, or I can't find any examples because it's bad design?

Answer Source

If you are using storyboards, you can create a new viewcontroller in the storyboard with the layout for your box. Then you can for example have this new viewcontroller be shown as a sheet, or a pop-up using a segue.

You can also use presentViewController in code to show the viewcontroller in a more custom fashion (using a custom animation for example).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download