2011-09-06 7 views
0

저는 뷰가 호출 될 때마다 IOS app 뷰에 대해 2 개의 UITextFeild와 1 UIButton을 프로그래밍 방식으로 생성했습니다. UIButton은 함수 (메소드) 로그인을 호출합니다. 두 UITextViews를 구별 할 수있는 방법을 만들 수 있습니까? 나는 JS 프로그래머이다. 같은 생각 패턴에서 나온 일부는 이드에 의해 객체를 얻는 방법 일까? 또는 모든 UITextFeilds이처럼 분할받을 : 사전에같은 클래스의 여러 객체를 처리하는 방법은 무엇입니까?

-(void)login{ 
Array *UITF[UITFself.view.UITextFeilds]; 
UITF[0].text; 
} 

감사

가장 쉬운 것은 헤더의 UITextField의를 선언하는 것입니다
UITextField *uname = [[UITextField alloc] initWithFrame:CGRectMake(80.0, 100, 150.0, 30.0)]; 
[uname addTarget:self 
      action:@selector(textFieldDone:) 
    forControlEvents:UIControlEventEditingDidEndOnExit]; 
uname.borderStyle = UITextBorderStyleRoundedRect; 
uname.keyboardType = UITextAutocapitalizationTypeNone; 
uname.returnKeyType = UIReturnKeyDone; 
uname.autocorrectionType = UITextAutocorrectionTypeNo; 
[self.view addSubview:uname]; 

-(void)login { 
    //send login message 
    NSString *u=uname.text;//user name text 
    NSString *p=pass.text;//password text 
    NSLog(u); 
    NSLog(p); 

} 
+1

'UITextView'와 'UITextField'는 다른 클래스이고'UITextFeild'는 철자 오류입니다. – PengOne

답변

1

:

UITextField *nameTextField; 
UITextField *passTextField; 

그런 다음 다음을 사용 구현의 참조

1

UITextFields를 만들 때 사용하는 변수를 인스턴스 변수로 저장하거나 더 좋게는 @property과 같이 헤더에 저장하고 로컬 변수는 저장하지 않습니다. 따라서 수업 중 어느 곳에서나 액세스 할 수 있습니다.

.H :

@interface YourClass : UIViewController { 

} 
@property(nonatomic, retain) IBOutlet UITextField* nameField; 
@property(nonatomic, retain) IBOutlet UITextField* passField; 
@end 

하는 .m : 참고로

@implementation YourClass 
@synthesize nameField, passField; 
-(void)dealloc { 
    self.nameField = nil; // release memory when your class is deallocated 
    self.passField = nil; // release memory when your class is deallocated 
    [super dealloc]; 
} 

-(void)loadView { 
    // here create your UITextFields programatically... 
    self.nameField = [[[UITextField alloc] initWitHFrame:...] autorelease]; 
    ... 
    self.passField = [[[UITextField alloc] initWitHFrame:...] autorelease]; 
    ... 

    // or much more easier, you should create them using InterfaceBuilder, 
    // this would save you a lot of code 
} 

-(IBAction)login { 
    NSLog(@"name: %@ ; pass: %@", nameField.text, passField.text); 
} 
@end 

, 당신은 당신의 인터페이스를 구축하는 인터페이스 빌더를 사용하는 경우 이미있을 것입니다, 당신은 문제가 없을 것으로주의 IBOutlets는 UITextFields에 연결되어 있습니다 (코드로 UITextFields를 만들고 구성하는 데 필요한 모든 코드를 저장했을 것입니다).


사이드 참고 : stringVariable가 귀하의 경우 (예를 들어, 어떤 지정자 문자 다음에 litteral '%'를 포함하는 경우 때문에 텍스트가 nameField에 입력 한 경우, NSLog(stringVariable); 사용하지만 오히려 NSLog(@"%@",stringVariable);하지 않습니다 , 사용자가 입력 한 "user % xyz"포함) 코드가 충돌합니다. 그래서 항상 변수가 아니라 NSLog의 첫 번째 매개 변수로 정적으로 입력 된 litteral 문자열을 사용하십시오.

0

UIView 개체에는 동적으로 생성 된보기의 경우 유용 할 수있는 NSInteger 태그 속성이 있습니다. 생성시 값을 설정하고 상위 뷰에서 viewWithTag :를 사용하십시오.

관련 문제