2011-08-11 73 views
0

我有一個應用程序,其中我有一個uiview上有兩個按鈕。我已經設置了我的uiview框架。如何獲得uiview顯示在按鈕點擊iphone

newview = [[UIView alloc]initWithFrame:CGRectMake(0,350,320,70)]; 

並將其設置爲我的主要觀點

[self.view addSubview:newview]; 

然後我添加了兩個按鈕,這個新的觀點。新視圖右側的新按鈕和其他按鈕上的一個按鈕。

這是我的按鈕

leftnavbutton= [[UIButton buttonWithType:UIButtonTypeCustom] retain]; 
      leftnavbutton.backgroundColor = [UIColor clearColor]; 
      [leftnavbutton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal ]; 

UIImage *buttonImageleft = [UIImage imageNamed:@"previous_large.png"]; 
      //UIImage *strechableButtonImageNormal = [buttonImageNormal stretchableImageWithLeftCapWidth:12 topCapHeight:0]; 

[leftnavbutton setBackgroundImage:buttonImageleft forState:UIControlStateNormal]; 
      [leftnavbutton addTarget:self action:@selector(playAction:) forControlEvents:UIControlEventTouchUpInside]; 
[leftnavbutton setFrame:CGRectMake(23, 28, 8, 44)]; 
      [newview addSubview:leftnavbutton]; 



rightnavbutton= [[UIButton buttonWithType:UIButtonTypeCustom] retain]; 
      rightnavbutton.backgroundColor = [UIColor clearColor]; 
      [rightnavbutton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal ]; 

UIImage *buttonImageright = [UIImage imageNamed:@"next_large.png"]; 
      //UIImage *strechableButtonImageNormal = [buttonImageNormal stretchableImageWithLeftCapWidth:12 topCapHeight:0];    

[rightnavbutton setBackgroundImage:buttonImageright forState:UIControlStateNormal];   

[rightnavbutton addTarget:self action:@selector(play:) forControlEvents:UIControlEventTouchUpInside];   

[rightnavbutton setFrame:CGRectMake(275, 28, 8, 44)]; 
[newview addSubview:rightnavbutton]; 

代碼,所以現在我想,當我對這些按鈕點擊。一種新觀點應該取代我的新觀點。 例如,現在當我在新視圖中運行我的應用程序時,將顯示當前從xml獲取的條件。所以當我點擊右邊的按鈕時,應該打開一個新的視圖,它應該獲取forecat條件標籤fron xml並顯示在它上面,當我點擊右邊的按鈕時它應該再次顯示當前的條件。即它應該在循環中。如果我點擊左邊的按鈕,則應顯示預測條件。這怎麼可能?

+0

您是否實施了播放和播放操作?對於每個視圖也有更好的控制器。 –

+0

@praveen不,我沒有實施遊戲動作 – Rani

+0

視圖控制器會工作得更好,並且更容易實現。試試吧...... –

回答

1

您只需用另一個視圖替換newView即可。要做到這一點,你應該首先設置newView的tag

newview = [[UIView alloc]initWithFrame:CGRectMake(0,350,320,70)]; 
newView.tag = 7; // set whatever number you want 
[self.view addSubview:newview]; 

playAction,得到newView的保持和從superView刪除它。然後在其位置添加另一個視圖。

-(void)playAction:(id)sender 
{ 
    UIView* newView = [self.view viewWithTag:7]; 
    [newView removeFromSuperView]; 

    //create and add the new new view that should replace new view. Use the same frame. 
    UIView* newnewview = [[UIView alloc]initWithFrame:CGRectMake(0,350,320,70)]; 
    newnewView.tag = 7; // set whatever number you want 
    [self.view addSubview:newnewview]; 

     //Add the left & right buttons again, as they were removed when newView was removed 
     ... code to add buttons... 
} 

類似地,可以實施play:方法。

+0

嗨akshay,因爲你告訴按鈕點擊視圖類被調用,但我有一個概率。我正在獲取tableview的行值並在uiview中顯示,但爲了顯示目的,tableview保持隱藏.so當我在switch case中調用indexpath.row時,我的斷點沒有進入它。 – Rani

+0

對不起,我不明白桌子從哪裏進來?我以爲你想在按下幾個按鈕時改變視圖。 – Akshay