Custom input view in Swift

I spent hours trying to figure out how to create / then get a custom inputView to work. I have a TextInputs (think scrabble board) grid that when clicked should load a custom inputView to insert text.

I created a .xib file containing UI elements for a custom inputView . I was able to create a CustomInputViewController and show the inputView , but I was never able to get the actual TextInput to update its value / text .

Apple's documentation has shown that it works, and many of the tutorials that I have seen use Obj-C (which I could not convert due to small things that, it seems, now cannot be executed in fast).

What is the comprehensive architecture and the necessary code fragments that must be implemented to create customInputView for several TextInputs (delegate chain, controller, .xibs, views, etc.)?

+5
source share
2 answers

Customize the nib file with the appropriate input layout and input elements. In my case, I set each button to act with the file owner of inputViewButtonPressed .

Set up a storyboard (or, if you want, nib, if you want) for the view controller.

Then, using the following code, you should get what you are looking for:

 class ViewController: UIViewController, UITextFieldDelegate { var myInputView : UIView! var activeTextField : UITextField? override func viewDidLoad() { super.viewDidLoad() // load input view from nib if let objects = NSBundle.mainBundle().loadNibNamed("InputView", owner: self, options: nil) { myInputView = objects[0] as UIView } // Set up all the text fields with us as the delegate and // using our input view for view in self.view.subviews { if let textField = view as? UITextField { textField.inputView = myInputView textField.delegate = self } } } func textFieldDidBeginEditing(textField: UITextField) { activeTextField = textField } func textFieldDidEndEditing(textField: UITextField) { activeTextField = nil } @IBAction func inputViewButtonPressed(button:UIButton) { // Update the text field with the text from the button pressed activeTextField?.text = button.titleLabel?.text // Close the text field activeTextField?.resignFirstResponder() } } 

Alternatively, if you want to be more like a keyboard, you can use this function as your action (it uses the new let syntax from Swift 1.2), break it down if you need 1.1 compatibility:

  @IBAction func insertButtonText(button:UIButton) { if let textField = activeTextField, title = button.titleLabel?.text, range = textField.selectedTextRange { // Update the text field with the text from the button pressed textField.replaceRange(range, withText: title) } } 

The UITextInput protocol is UITextInput to update a text field. Deletion handling is a bit more complicated, but still not so bad:

  @IBAction func deleteText(button:UIButton) { if let textField = activeTextField, range = textField.selectedTextRange { if range.empty { // If the selection is empty, delete the character behind the cursor let start = textField.positionFromPosition(range.start, inDirection: .Left, offset: 1) let deleteRange = textField.textRangeFromPosition(start, toPosition: range.end) textField.replaceRange(deleteRange, withText: "") } else { // If the selection isn't empty, delete the chars in the selection textField.replaceRange(range, withText: "") } } } 
+9
source

You do not have to go through all these chores. IOS 8 introduced a new class: UIAlertController , where you can add TextFields for user input. You can create it as an alert or ActionSheet.

Example:

 let alertAnswer = UIAlertController(title: "Input your scrabble Answer", message: nil, preferredStyle: .Alert) // or .ActionSheet 

Now that you have the controller, add the fields to it:

 alertAnswer.addTextFieldWithConfigurationHandler { (get) -> Void in getAnswer.placeholder = "Answer" getAnswer.keyboardType = .Default getAnswer.clearsOnBeginEditing = true getAnswer.borderStyle = .RoundedRect } // add as many fields as you want with custom tweaks 

Add action buttons:

 let submitAnswer = UIAlertAction(title: "Submit", style: .Default, handler: nil) let cancel = UIAlertAction(title: "Cancel", style: .Cancel, handler: nil) alertAnswer.addAction(submitAnswer) alertAnswer.addAction(cancel) 

Introduce the controller whenever you want:

 self.presentViewController(alertAnswer, animated: true, completion: nil) 

As you can see, you have various handlers for passing custom code at any stage.

As an example, it will look like this: An example of how it could look

+7
source

Source: https://habr.com/ru/post/1205658/


All Articles