user3088011
user3088011

Reputation: 23

Add UItextfield on button click

I'm new to Objective-c and i've reached an issue.

I'm trying to make a small scoreboard app, for learning more of the language. Here is a small part of the app.

example 1

What i want it it to do is to add a new UItext field when you press the add button. So on button click it looks like this.

example 2

How can i add multiple UItextfields on button click in objective-c?

Upvotes: 1

Views: 665

Answers (1)

LuisCien
LuisCien

Reputation: 6432

This is a very simple requirement. All you have to do is to create and add the new "Player Number" text field and "Fouls" label each time you tap the "Add" button. After this, move the "Add" button downwards and that's it. Here is some example code.

First of all, to keep things simple I created a UIViewController subclass to hold the "Player Number" text box and the "Fouls" label to make it easy to create them multiple times. Here is the class:

// The .h file
#import <UIKit/UIKit.h>

@interface LLPlayerViewController : UIViewController
// This is a very simple class with no public properties or methods
@end

// The .m file
@implementation LLPlayerViewController

-(void)loadView {
    // Create the initial view
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 190, 25)];
    self.view = view;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    // PlayerNumber Text Field
    UITextField *playerNumberField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 120, 20)];
    playerNumberField.placeholder = @"PlayerNumber";
    playerNumberField.borderStyle = UITextBorderStyleLine;
    [self.view addSubview:playerNumberField];

    // Fouls Label
    UILabel *foulsLabel = [[UILabel alloc] initWithFrame:CGRectMake(135, 0, 50, 20)];
    foulsLabel.text = @"0";
    [self.view addSubview:foulsLabel];
}

@end

Then, in the main View Controller I create instances of that class and add them to the view each time I press the "Add" button. Here is some sample code:

// Don't forget to import the previous class
#import "LLPlayerViewController.h"

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Inside viewDidLoad add the following code:
    self.playersArray = [[NSMutableArray alloc] init];

    // PLAYER Header
    UILabel *playerLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 25, 80, 20)];
    playerLabel.text = @"PLAYER";
    [self.view addSubview:playerLabel];

    // FOULS Header
    UILabel *foulsLabel = [[UILabel alloc] initWithFrame:CGRectMake(145, 25, 80, 20)];
    foulsLabel.text = @"FOULS";
    [self.view addSubview:foulsLabel];

    // Add player button
    self.addPlayerButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    self.addPlayerButton.frame = CGRectMake(30, 60, 80, 30);
    [self.addPlayerButton setTitle:@"Add" forState:UIControlStateNormal];
    [self.addPlayerButton addTarget:self action:@selector(addPlayerTapped:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.addPlayerButton];
}

// This method will get called each time the user taps the "Add" button
-(void)addPlayerTapped:(id)sender {

    NSLog(@"Adding Player");
    self.yOffset = 60.0;

    LLPlayerViewController *llVC = [[LLPlayerViewController alloc] init];
    llVC.view.alpha = 0.0;
    [self.playersArray addObject:llVC];
    [self.view addSubview:llVC.view];

    [self repositionFields];
}

// This method is responsible for repositioning the views
-(void)repositionFields {

    // Reposition each view in the array
    for (LLViewController *llVC in self.playersArray) {
        CGRect frame = llVC.view.frame;
        frame.origin.x = 15;
        frame.origin.y = self.yOffset;
        llVC.view.frame = frame;
        self.yOffset += frame.size.height;
    }

    // Add some animations to make it look nice
    [UIView animateWithDuration:0.3 animations:^{
        CGRect frame = self.addPlayerButton.frame;
        frame.origin.y = self.yOffset;
        self.addPlayerButton.frame = frame;
    } completion:^(BOOL finished) {
        LLViewController *llVC = [self.playersArray lastObject];
        llVC.view.alpha = 1.0;
    }];
}

This code was tested on an iPad simulator but I believe it would also work on an iPhone. The code is straight-forward and self explanatory but let me know if you have any more questions.

Hope this helps!

Upvotes: 2

Related Questions