2014-10-29 4 views
0

내가 동적으로 세 개의 단추와 하나 uitextview을 만드는하지만 난이 프로그램을 실행할 때의 내 화면내 버튼과 textview가 iOS의 화면에 표시되지 않습니까?

당신이 self.view를 할당하는이 내 동적 버튼을 만들 수있는 코드와 텍스트 뷰

-(void)getSmsdisplay 

{ 
    self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] 
    applicationFrame]]; 
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    UITextView *textview=[[UITextView alloc]init]; 
    [button setTitle:@"Comment" forState:UIControlStateNormal]; 
    [button1 setTitle:@"Share" forState:UIControlStateNormal]; 
    [button1 setTitle:@"Like" forState:UIControlStateNormal]; 

    //listen for clicks 
    [button addTarget:self 
    action:@selector(btncomment:)forControlEvents:UIControlEventTouchUpInside]; 
    [button1 addTarget:self 
    action:@selector(btnShare:)forControlEvents:UIControlEventTouchUpInside]; 

    [button2 addTarget:self 
    action:@selector(Like:)forControlEvents:UIControlEventTouchUpInside]; 

    smsdata *data=[[smsdata alloc]init]; 

    [textview setText:data.Data]; 
    [self.view addSubview:textview]; 



    //add the button to the view 
    [self.view addSubview:button]; 
    [self.view addSubview:button1]; 
    [self.view addSubview:button2]; 

    } 
+0

입니다 다른 것의 하위보기. – ZeMoon

답변

1

당신의 코딩은 괜찮지 만, UR은 UIButton에 대한 프레임을 추가하지 UItextview

-(void)getSmsdisplay 

{ 
self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] 
              applicationFrame]]; 
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
UIButton *button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 


UITextView *textview=[[UITextView alloc]init]; 

button.frame=CGRectMake(20, 50, 200, 50); 

    button1.frame=CGRectMake(20, 150, 200, 50); 

    button2.frame=CGRectMake(20, 300, 200, 50); 

button.backgroundColor=[UIColor redColor]; 
    button1.backgroundColor=[UIColor grayColor]; 
    button2.backgroundColor=[UIColor blackColor]; 

[button setTitle:@"Comment" forState:UIControlStateNormal]; 
[button1 setTitle:@"Share" forState:UIControlStateNormal]; 
[button2 setTitle:@"Like" forState:UIControlStateNormal]; 

//listen for clicks 
[button addTarget:self 
      action:@selector(btncomment:)forControlEvents:UIControlEventTouchUpInside]; 
[button1 addTarget:self 
      action:@selector(btnShare:)forControlEvents:UIControlEventTouchUpInside]; 

[button2 addTarget:self 
      action:@selector(Like:)forControlEvents:UIControlEventTouchUpInside]; 



[textview setText:@"karthik"]; 
[self.view addSubview:textview]; 



//add the button to the view 
[self.view addSubview:button]; 
[self.view addSubview:button1]; 
[self.view addSubview:button2]; 

} 

출력은 당신으로 추가되지 않은 다른 self.view를 재 할당하는 enter image description here

+0

가로로 내 창 아래쪽에이 버튼을 넣고 싶습니다. –

+0

그냥 프레임을 변경하면 –

+0

은 제가 –

1

에 표시하고 있지 않다 다른 UIView 인스턴스는 viewController에 추가되지 않았습니다. self.view는 viewController 내에서 이미 초기화되어 있으므로 재 할당하는 것은 위험합니다.

또한 단추 및 텍스트 뷰에 대한 프레임을 설정하지 않았습니다.

다음 코드는 제대로 작동해야합니다.

-(void)getSmsdisplay 

{ 
    UIView *smsView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] 
    applicationFrame]]; 
    [self.view addSubview: smsView]; //The new view needs to be added to something! 

    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    UITextView *textview=[[UITextView alloc]initWithFrame:CGRectMake:(20,20,100,50)]; 
    [button setTitle:@"Comment" forState:UIControlStateNormal]; 
    [button1 setTitle:@"Share" forState:UIControlStateNormal]; 
    [button1 setTitle:@"Like" forState:UIControlStateNormal]; 

    //listen for clicks 
    [button addTarget:self 
    action:@selector(btncomment:)forControlEvents:UIControlEventTouchUpInside]; 
    [button1 addTarget:self 
    action:@selector(btnShare:)forControlEvents:UIControlEventTouchUpInside]; 

    [button2 addTarget:self 
    action:@selector(Like:)forControlEvents:UIControlEventTouchUpInside]; 

    smsdata *data=[[smsdata alloc]init]; 

    [textview setText:data.Data]; 
    [smsView addSubview:textview]; 

    button.frame=CGRectMake(20, 50, 200, 50); 
    button1.frame=CGRectMake(20, 150, 200, 50); 
    button2.frame=CGRectMake(20, 300, 200, 50); 

    //add the button to the view 
    [smsView addSubview:button]; 
    [smsView addSubview:button1]; 
    [smsView addSubview:button2]; 

    } 
+0

질문에 대한 지원을위한 tanx, 답변도 괜찮습니다. –

관련 문제