Search This Blog

Thursday, August 20, 2015

Create a text field, button programmatically

    UITextField *userNameField = [[UITextField alloc] initWithFrame:CGRectMake(20, 30, 300, 40)];
    userNameField.placeholder = @"userName";
    userNameField.accessibilityLabel = @"UserName Field";
    userNameField.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:userNameField];
   
   
    UITextField *passwordField = [[UITextField alloc] initWithFrame:CGRectMake(20, 90, 300, 40)];
    passwordField.placeholder = @"password";
    passwordField.secureTextEntry = YES;
    passwordField.accessibilityLabel = @"Password Field";
    passwordField.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:passwordField];
   
   
    UIButton * submitButton = [UIButton buttonWithType:UIButtonTypeSystem];
    submitButton.frame = CGRectMake(20, 150, 150, 40);
    [submitButton setTitle:@"Submit" forState:UIControlStateNormal];
    submitButton.accessibilityLabel = @"Submit Button";
    [submitButton addTarget:self action:@selector(submitButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    submitButton.backgroundColor = [UIColor redColor];
    [submitButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.view addSubview:submitButton];

Implementation example of button click:

-(void)submitButtonPressed:(id)Sender{
    [self dismissViewControllerAnimated:YES completion:nil];
}

-(void)loginButtonPressed:(id)sender{
    LoginViewController *loginViewController = [LoginViewController new];   // create a modal
    [self presentViewController:loginViewController animated:YES completion:nil];  // preent it as a model
}

No comments:

Post a Comment