按钮单击时添加UItextfield

时间:2013-12-17 20:21:55

标签: objective-c

我是Objective-c的新手,我遇到了一个问题。

我正在尝试制作一个小型记分牌应用程序,以便学习更多语言。 这是应用程序的一小部分。

example 1

我想要它做的是在按下添加按钮时添加新的UItext字段。所以在按钮上单击它看起来像这样。

example 2

如何在objective-c中按钮点击添加多个UItextfield?

1 个答案:

答案 0 :(得分:2)

这是一个非常简单的要求。您所要做的就是每次点击“添加”按钮时创建并添加新的“玩家号码”文本字段和“污垢”标签。在此之后,向下移动“添加”按钮即可。这是一些示例代码。

首先,为了简单起见,我创建了一个UIViewController子类来保存“Player Number”文本框和“Fouls”标签,以便于多次创建它们。这是班级:

// 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

然后,在主视图控制器中,我创建该类的实例,并在每次按“添加”按钮时将它们添加到视图中。以下是一些示例代码:

// 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;
    }];
}

此代码在iPad模拟器上进行了测试,但我相信它也适用于iPhone。该代码是直截了当且不言自明的,但如果您有任何其他问题,请告诉我。

希望这有帮助!