2013-01-11 5 views
-1

저는 iOS 및 xcode에 새로 입문되었습니다. nib 파일이나 스토리 보드를 사용하지 않고 텍스트 필드를 만들고 싶습니다. 나는 답을 찾았지만 어디서나 혼란스러워하거나 펜촉 파일을 사용했다. 나는 문제를 직면하고있는 텍스트 필드 m을 위해 버튼과 그 일 벌금을 만들었다. 여기 내 viewcontroller.m 도와주세요.ios 용 텍스트 필드

#import "ViewController.h" 

@interface ViewController() 

@end 

@implementation ViewController 

- (void)loadView { 


self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]]; 
self.view.backgroundColor = [UIColor purpleColor]; 


UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
UITextField *textfield; 


button.frame = CGRectMake(10 ,350 , 300, 40); 
button1.frame = CGRectMake(10 ,50 , 300, 40); 
textfield.frame = CGRectMake(10, 100, 200, 30); 


[button setTitle:@"Change screen to green!" forState:UIControlStateNormal]; 
[button1 setTitle:@"click screen to red!" forState:UIControlStateNormal]; 

[button addTarget:self action:@selector(buttonPressed)  forControlEvents:UIControlEventTouchUpInside]; 
[button1 addTarget:self action:@selector(buttonPressed1) forControlEvents:UIControlEventTouchUpInside]; 


[self.view addSubview:button]; 
[self.view addSubview:button1]; 
[self.view addSubview:textfield]; 
} 
-(void)buttonPressed { 
self.view.backgroundColor = [UIColor greenColor]; 

} 
-(void)buttonPressed1 { 
self.view.backgroundColor = [UIColor redColor]; 

} 
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); 
} 

@end 

감사 & 관련

답변

0

대답이 제시 한 것처럼 alloc init을 추가했다면 실제로 텍스트 필드가 화면에 표시되지만 테두리 나 텍스트가 없으므로 텍스트 필드를 볼 수 없습니다. 이 줄을 추가 시도하고 당신이 그것을 볼 수 있습니다 :

textfield.borderStyle = UITextBorderStyleRoundedRect; 
+0

덕분에 .. 지금 일하고있어. :) – Newbee

0

사용이 : 반대하지

UITextField *textfield; 

UITextField *textfield; 그냥 크레타 객체 참조의

UITextField *textfield = [[UITextField alloc] init]; 

Insetd.

+0

감사를 해결할 수 있지만 아직 난 내 화면에 텍스트 필드를 볼 수 없습니다. – Newbee

+0

여기에서 코드를 업데이트하여 진행 상황을 볼 수 있습니다. – CRDave

+0

UITextField * textfield = [[UITextField alloc] init]; textfield.frame = CGRectMake (10, 100, 200, 30); textfield.delegate = 자기; [self.view addSubview : textfield]; } - (BOOL) shouldAutorotateToInterfaceOrientation : (UIInterfaceOrientation) interfaceOrientation { 리턴 (! interfaceOrientation = UIInterfaceOrientationPortraitUpsideDown); } @end – Newbee

1

방금 ​​할당하지 않고 UITextField을 선언했습니다.

UITextField *txt = [[UITextField alloc] init]; 
txt.frame = CGRectMake(10, 100, 200, 30); 
txt.delegate = self; 
[self.view adsubview:txt]; 

그것은 당신의 문제를 해결 - 당신은 먼저 frame &이 같은 view에 추가 설정 할당해야합니다.

+0

이것은 @ Shanu Singh – iProgrammed

+0

에게 @Girish에게 감사의 말을 고쳐주는 것 같습니다. 하지만 내가 대리자를 사용하여 경고 메시지를 던지고 화면에 텍스트 필드를 볼 수 없습니다. – Newbee

+0

@interface SearchWineViewController : UIViewController 와 같은 .h 파일에서 UITextFieldDelegate를 설정하고 .m 파일의 대리자 메서드를 구현합니다. 또한 텍스트 필드를 표시 할 적절한 프레임을 설정하십시오. – Girish

1

먼저 사용

UITextField *textfield = [[UITextField alloc] init]; 

대신

UITextField *textfield; 

의 다음 clearColor 될 것입니다 배경에서 색상 때문에 텍스트 필드의 배경에서 색상을 설정 기본적으로.

textfield.backgroundColor = [UIColor whiteColor]; 

또는

textfield.borderStyle = UITextBorderStyleRoundedRect; 

는 화이트 컬러의 보더 텍스트 필드를 얻기 위해 사용합니다. 내가 그랬 @CRDave

0
UITextField *textfield =[[UITextField alloc] init]; 
textfield.frame = CGRectMake(10, 100, 200, 30); 
textfield.delegate = self; [self.view addSubview:textfield]; 
[self.view bringSubViewToFront:textfield]; 

문제를