قالب وردپرس درنا توس
Home / IOS Development / An iOS notification display with a text box and a "smart" OK button – Ole Begemann

An iOS notification display with a text box and a "smart" OK button – Ole Begemann



I wrote a handy little excerpt for creating a UIAlertController with a single text field to request an input from the user. The main feature next to a slightly finer API: You can pass in a validation rule that continually checks whether the entry is valid when the user types text. The OK button will only be activated when the input is approved.

Here is a demo that uses the validation rule .nonEmpty ie. The entry must not be empty:


  Demo of UIAlertController with the text field in the iOS simulator
Notice how the submission button will be disabled automatically when the text field is empty.

The Public Interface is a UIAlertController initializes with the following signature:

  extension    UIAlertController    {
      public    convenience    init   ([1
9659013] ] Title
: String          message : String = nil          cancelButtonTitle : String          okButtonTitle : String validate validationRule . TextValidationRule = [19659038] noRestriction         ] ] = ] -> void          onCompletion : @escaping ( TextInputResult ) -> void ) { . .. } }

Init ializer takes seven arguments. Four are the usual strings for title, message and button labels. The others need some explanation:

  • validationRule Describes which inputs should be accepted. There are four types of validation rules: (1) there are no limitations, ie any entry is valid, including the empty string; (2) the entrance must be empty; (3) You can specify a regular expression that should match the input string; or (4) you can pass in a predicate function that determines if the entry is valid. Here is the definition of enum describing these options:

      public    enum    TextValidationRule    {
          case    noRestriction 
          case    nonEmpty 
          case    regular     ] ] 
          case    preached   ((  String )    ->    Boolean ) 
      
          public    FUNC    IsValid   (  _    entry :    String )    ->    Boolean    {
              / / return true if the entry string passes validation 
        } 
    } 
    

    You can use this to validate email addresses using a regular expression:

      la    emailRegex    =    ]attempt !    NSRegularExpression   ( ] :    ". + +   \   .. +" ) 
      la    emailRule    =    TextValidationRule . [19659121] Regular Expression   (  EmailRegex ) 
      EmailRule .   IsValid   (  "alice@example.com" )    // → 19659121]     isValid   (  "bob @ gmail" [[19659013])    // → false 
    

    Or just accept numbers using a predicate:

      la    integerRule    =    TextValidationRule .    ]  ]         $ 0 ]  ! = ] 
    ] 
      integerRule .   
      
      
      "- 789"   ]    // → true 
      integerRule    isValid   (  "123a" )    ] // → false 
    
  • textFieldConfiguration An optional feature that you can pass to configure the text field. Use this to insert a placeholder text, configure the keyboard type, etc.

  • onCompletion The callback invoked when the user denies notification by pressing a button. The reported result is either .cancel (for the Cancel button) or .ok (String) where the corresponding value is the text the user entered. I defined this one for the return value:

      public    enum    TextInputResult    {
          case    cancel [19659062] Case    ok   (  String [19659012]) 
    } 
    

The full code is available as Hostage. It's quite long (more than 100 lines, including doc comments), so let's take it apart step by step.

After calling super we first define an internal aid class to serve as the text field's delegate and target goal. We also declare a local variable for storing an instance of the text field observer:

  public    convenience    init   ( ... ) 
{
init ( title ~~ POS = TRUNC . title ~~ POS = TRUNC message : message ] preferredStyle [19659012] . Notice )

/// Observes an UITextField for different events and reports them via callbacks.
/// Specifies as the text field's delegate and target goal. {FIFA 19659013] [19659000]] [19659000]] NSF [19659000] UITextFieldDelegate {
la textFieldValueChanged : ( UITextField


Source link