iOS Code Snippet Input-Dialog

Filippo

Expert
Licensed User
Hi,

I extended the code of Erel and turned into a proper input-dialog. :)

Insert the code into a code-module.
B4X:
#Region Input-Dialog with Objective C code
#If OBJC
- (void)ShowInputDialog:(NSString*)Input :(NSString*)Title :(NSString*)Message :(NSString*)OK :(NSString*)Cancel :(int)KeybardType
{
  UIAlertView * alert = [[UIAlertView alloc] initWithTitle:Title
    message:Message delegate:self cancelButtonTitle:Cancel otherButtonTitles:OK, nil];
    alert.alertViewStyle = UIAlertViewStylePlainTextInput;

    UITextField * alertTextField = [alert textFieldAtIndex:0];
    alertTextField.keyboardType = KeybardType;
   
    alertTextField.text = Input;
    alert.delegate = self;
    [alert show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1) {
        [self.bi raiseEvent:nil event:@"inputdialog_result:" params:@[[[alertView textFieldAtIndex:0] text]]];
    }
}
#End If

' InputType:
' 0 = UIKeyboardTypeDefault
' 1 = UIKeyboardTypeASCIICapable
' 2 = UIKeyboardTypeNumbersAndPunctuation
' 3 = UIKeyboardTypeURL
' 4 = UIKeyboardTypeNumberPad
' 5 = UIKeyboardTypePhonePad
' 6 = UIKeyboardTypeNamePhonePad
' 7 = UIKeyboardTypeEmailAddress
' 8 = UIKeyboardTypeDecimalPad
' 9 = UIKeyboardTypeTwitter
'10 = UIKeyboardTypeWebSearch
'11 = UIKeyboardTypeAlphabet
'12 = UIKeyboardTypeASCIICapable
Sub InputDialog(Input As String, Title As String, Message As String, Positive As String, Cancel As String, InputType As Int)
    Dim no As NativeObject = Me
    no.RunMethod("ShowInputDialog::::::", Array(Input, Title, Message, Positive, Cancel, InputType))
End Sub

Sub InputDialog_Result(Text As String)
    Log("Result: " & Text)
    Select strInputDialog
        Case "1"
            txtUserName.text=Text
        Case "2"
            ...
    End Select
End Sub
#End Region
Usage:
B4X:
Sub Process_Globals
  Dim strUserName As String
  Dim strInputDialog As String
  Dim txtUserName As TextField
End Sub

Private Sub Application_Start (Nav As NavigationController)
...
...
    strInputDialog = "1"
    InputDialog(strUserName, "Username", "Please enter a name", "OK", "Cancel", 0)
End Sub
 

Filippo

Expert
Licensed User

jazzzzzzz

Active Member
Licensed User
For getting button event, replace this method in above sample
B4X:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSString *inStr = [NSString stringWithFormat: @"%ld", (long)buttonIndex];
    [self.bi raiseEvent:nil event:@"inputdialog_result::" params:@[([[alertView textFieldAtIndex:0] text]),(inStr)]];
}
 
Last edited:
Top