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。該代碼是直截了當的和自我解釋,但讓我知道如果你有任何問題。
希望這會有所幫助!