1
我是Objective-c的新手,我遇到了问题。按钮点击添加UItextfield
我正在尝试制作一个小记分牌应用程序,以便学习更多的语言。 这是应用程序的一小部分。
我想它,它做的是,当你按添加按钮添加一个新的UItext场。所以在按钮上单击它看起来像这样。
我怎样才能在Objective-C的按钮,点击添加多个UItextfields?
我是Objective-c的新手,我遇到了问题。按钮点击添加UItextfield
我正在尝试制作一个小记分牌应用程序,以便学习更多的语言。 这是应用程序的一小部分。
我想它,它做的是,当你按添加按钮添加一个新的UItext场。所以在按钮上单击它看起来像这样。
我怎样才能在Objective-C的按钮,点击添加多个UItextfields?
这是一个非常简单的要求。您只需在每次点击“添加”按钮时创建并添加新的“球员号码”文本框和“犯规”标签。之后,向下移动“添加”按钮即可。这里是一些示例代码。
首先,为了简单起见,我创建了一个UIViewController
子类,以容纳“播放器号码”文本框和“犯规”标签,以便于多次创建它们。这里是类:
// 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。该代码是直截了当的和自我解释,但让我知道如果你有任何问题。
希望这会有所帮助!