Rani
Rani

Reputation: 3453

How to get a uiview to be displayed on button click in iphone

i have an application in which i have a uiview which has a two buttons on it. i have set the frame of my uiview like this.

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

and set it to my main view

[self.view addSubview:newview];

then to this new view i have added two buttons. one button on the left side of new and other button of right side of new view.

this is my code for button

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];  

so now i want when i click on these buttons. a new view should replace over my new view. for example now when i run my application in the new view current conditon which is fetched from xml is displayed in that. So when i click on the right button a new view should open which should fetch the forecsat condition tag fron xml and display on it and when i click on the right button it should again display the current condition. i.e it should be in loop. if i click on the left button againg forecast condition should be shown. How is this possible?

Upvotes: 0

Views: 2074

Answers (1)

Akshay
Akshay

Reputation: 5765

All you need to do is replace newView with another view. To do that, you should first set the newView's tag.

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

In playAction, get hold of the newView and remove it from the superView. Then add another view in its place.

-(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...
}

Similarly the play: method can be implemented.

Upvotes: 1

Related Questions