Implementing a Custom UI Form for Multiple Text Inputs in Xcode iPhone
Introduction
In this article, we will explore how to create a custom CatchNames class that can be imported into a view controller to collect multiple text inputs from the user. The class will prompt the user to enter three names in a row and return them as an array. We will also discuss the best practices for implementing a simple form with UI elements like UITextFields, UIButtons, and UIViews.
Understanding the Problem
The problem at hand is to create a custom class that can be used to collect multiple text inputs from the user. The class should prompt the user to enter three names in a row and return them as an array when the user clicks the “Done” or “Add” button. We will assume that this is a simple task, but we will also provide guidance on how to implement it correctly.
Designing the CatchNames Class
The CatchNames class should have the following properties:
- An instance variable to store the array of user-inputted names
- An instance variable to store the index of the current name being entered by the user
- An initializer method to set up the UI elements and start prompting the user for input
Here is an example implementation of the CatchNames class:
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface CatchNames : NSObject
@property (nonatomic, strong) NSMutableArray *namesArray;
@property (nonatomic, assign) NSUInteger currentNameIndex;
- (instancetype)initWithViewController:(UIViewController *)viewController;
@end
#import "CatchNames.h"
@implementation CatchNames
- (instancetype)initWithViewController:(UIViewController *)viewController {
self = [super init];
if (self) {
// Initialize the namesArray and currentNameIndex instance variables
self.namesArray = [[NSMutableArray alloc] initWithCapacity:3];
self.currentNameIndex = 0;
// Create the UI elements for the form
UITextField *nameField1 = [[UITextField alloc] init];
nameField1.placeholder = @"Name 1";
[viewController.view addSubview:nameField1];
UITextField *nameField2 = [[UITextField alloc] init];
nameField2.placeholder = @"Name 2";
[viewController.view addSubview:nameField2];
UIButton *doneButton = [[UIButton alloc] init];
doneButton.setTitle:@"Done", forState:UIControlStateNormal);
[doneButton addTarget:self action:@selector(doneButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[viewController.view addSubview:doneButton];
}
return self;
}
- (void)doneButtonPressed:(id)sender {
// Get the text from the current name field
NSString *name = [[self.nameField1 text] trimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
// Add the name to the namesArray and increment the currentNameIndex
if (name.length > 0) {
[self.namesArray addObject:name];
self.currentNameIndex++;
// If all three fields are filled, disable the done button and enable the next field
if (self.currentNameIndex == 3) {
[[self.nameField1 text] setString:@""];
[[self.nameField2 text] setString:@""];
[[self.nameField3 text] setString:@""];
self.nameField1.enabled = NO;
self.nameField2.enabled = NO;
// Enable the done button
[doneButton setTitle:@"Submit", forState:UIControlStateNormal];
}
}
// Clear the name field and enable it again for the next iteration
[[self.nameField1 text] setString:@""];
[[self.nameField1 text].mutableStringByTrimmingCharactersInSet([NSCharacterSet whitespaceAndNewlineCharacterSet]) setString:@"Name 1"];
self.nameField1.enabled = YES;
}
Implementing the View Controller
The view controller should import the CatchNames class and create an instance of it. It should also add the UI elements to its view.
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@property (nonatomic, strong) CatchNames *catchNames;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Create an instance of the CatchNames class and set it as a property
self.catchNames = [[CatchNames alloc] initWithViewController:self];
// Add the UI elements to the view
UITextField *nameField1 = [[self.catchNames.nameField1 subview] superview];
nameField1.frame = CGRectMake(0, 100, 300, 30);
}
- (void)doneButtonPressed:(id)sender {
NSArray *namesArray = [self.catchNames.namesArray copy];
// Do something with the names array
NSLog(@"%@", namesArray);
// Disable the done button and enable the next field
[[self.catchNames.nameField1 text] setString:@"Name 1"];
self.catchNames.nameField1.enabled = YES;
// Enable the done button again
[sender setTitle:@"Done", forState:UIControlStateNormal];
}
Conclusion
In this article, we explored how to create a custom CatchNames class that can be used to collect multiple text inputs from the user. We designed the class and implemented its properties and methods. We also demonstrated how to use the class in a view controller and handle the button press event.
Tips and Variations
- To make the form more robust, you can add validation for each field to ensure that they are not empty.
- You can also add a reset button to clear all the fields and start fresh.
- To make the form more user-friendly, you can use Auto Layout to position the fields and buttons correctly on different screen sizes.
- You can also use a storyboard or xib file to design the UI elements instead of creating them programmatically.
Last modified on 2023-12-13