简体   繁体   中英

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?

您可以使用annexView将控件(您的情况下为TextField)添加到NSAlert。

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM