Created
December 25, 2019 05:51
-
-
Save Gandum2077/0c8cdc3e61784b849c59cbb7cd75a015 to your computer and use it in GitHub Desktop.
Create log-in dialog using Objective-C APIs in JSBox
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //---- Definition ----/ | |
| const UIAlertActionStyle = { | |
| Default: 0, | |
| Cancel: 1, | |
| Destructive: 2, | |
| } | |
| const UIAlertControllerStyle = { | |
| ActionSheet: 0, | |
| Alert: 1, | |
| } | |
| class UIAlertAction { | |
| constructor(title, style=UIAlertActionStyle.Default, handler) { | |
| this.title = title; | |
| this.style = style; | |
| this.instance = $objc("UIAlertAction").$actionWithTitle_style_handler(title, style, $block("void, UIAlertAction *", () => { | |
| if (handler) { | |
| handler(this); | |
| } | |
| })); | |
| } | |
| } | |
| class UIAlertController { | |
| constructor(title, message, style=UIAlertControllerStyle.ActionSheet) { | |
| this.title = title; | |
| this.message = message; | |
| this.style = style; | |
| this.instance = $objc("UIAlertController").$alertControllerWithTitle_message_preferredStyle(title, message, style); | |
| } | |
| addAction(action) { | |
| this.instance.$addAction(action.instance); | |
| } | |
| addTextField(options) { | |
| this.instance.$addTextFieldWithConfigurationHandler($block("void, UITextField *", textField => { | |
| textField.$setClearButtonMode(1); | |
| if (options.type) { | |
| textField.$setKeyboardType(options.type); | |
| } | |
| if (options.placeholder) { | |
| textField.$setPlaceholder(options.placeholder); | |
| } | |
| if (options.text) { | |
| textField.$setText(options.text); | |
| } | |
| if (options.textColor) { | |
| textField.$setTextColor(options.textColor.ocValue()); | |
| } | |
| if (options.font) { | |
| textField.$setFont(options.font.ocValue()); | |
| } | |
| if (options.align) { | |
| textField.$setTextAlignment(options.align); | |
| } | |
| if (options.secure) { | |
| textField.$setSecureTextEntry(true); | |
| } | |
| if (options.events) { | |
| const events = options.events; | |
| textField.$setDelegate($delegate({ | |
| type: "UITextFieldDelegate", | |
| events: { | |
| "textFieldShouldReturn:": textField => { | |
| if (events.shouldReturn) { | |
| return events.shouldReturn(); | |
| } else { | |
| return true; | |
| } | |
| } | |
| } | |
| })); | |
| } | |
| })); | |
| } | |
| getText(index) { | |
| const textField = this.instance.$textFields().$objectAtIndex(index); | |
| const text = textField.$text(); | |
| return text.jsValue(); | |
| } | |
| present() { | |
| this.instance.$show(); | |
| } | |
| } | |
| //---- Usage ----/ | |
| const alertVC = new UIAlertController("Log in", "Enter your user name and password:", UIAlertControllerStyle.Alert); | |
| alertVC.addTextField({ | |
| type: $kbType.ascii, | |
| placeholder: "User name" | |
| }); | |
| alertVC.addTextField({ | |
| placeholder: "Password", | |
| secure: true, | |
| events: { | |
| shouldReturn: () => { | |
| const userName = alertVC.getText(0); | |
| const password = alertVC.getText(1); | |
| const isValid = userName.length > 0 && password.length > 0; | |
| if (isValid) { | |
| login(); | |
| } | |
| return isValid; | |
| } | |
| } | |
| }); | |
| alertVC.addAction(new UIAlertAction("Cancel", UIAlertActionStyle.Destructive, null)); | |
| alertVC.addAction(new UIAlertAction("Enter", UIAlertActionStyle.Default, login)); | |
| alertVC.present(); | |
| function login() { | |
| const userName = alertVC.getText(0); | |
| const password = alertVC.getText(1); | |
| console.log(`${userName}, ${password}`); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment