2013-12-17 3 views
1

저는 Objective-C를 처음 접했고 문제가 발생했습니다.버튼 클릭시 UItextfield 추가

나는 더 많은 언어를 배우기 위해 작은 스코어 보드 응용 프로그램을 만들려고 노력하고 있습니다. 다음은 앱의 일부입니다. 내가 그것을 수행 할 작업을

example 1

는 추가 버튼을 누르면 새로운 UItext 필드를 추가하는 것입니다. 그래서 버튼을 클릭하면 다음과 같이 보입니다.

example 2

어떻게 목표 - C에서 버튼 클릭에 여러 UItextfields를 추가 할 수 있습니까?

답변

2

이것은 매우 간단한 요구 사항입니다. "Add"버튼을 누를 때마다 새로운 "Player Number"텍스트 필드와 "Fouls"레이블을 만들고 추가하기 만하면됩니다. 그 후에 "추가"버튼을 아래쪽으로 이동하면됩니다. 다음은 몇 가지 예제 코드입니다.

우선, 물건을 간단하게 만들기 위해 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에서도 작동 할 것이라고 생각합니다. 이 코드는 간단하고 직접 설명 할 수 있지만 더 궁금한 점이 있으면 알려주십시오.

희망이 도움이됩니다.

관련 문제