简体   繁体   English

如何在 iphone 中单击按钮时显示 uiview

[英]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.我有一个应用程序,其中有一个 uiview,上面有两个按钮。 i have set the frame of my uiview like this.我已经像这样设置了我的 uiview 的框架。

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.例如,现在当我在新视图中运行我的应用程序时,从 xml 获取的当前条件显示在其中。 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.因此,当我单击右侧按钮时,应打开一个新视图,该视图应从 xml 获取预测条件标签并显示在其上,当我单击右侧按钮时,它应再次显示当前条件。 ie it should be in loop.即它应该在循环中。 if i click on the left button againg forecast condition should be shown.如果我再次单击左侧按钮,则应显示预测条件。 How is this possible?这怎么可能?

All you need to do is replace newView with another view.您需要做的就是将newView替换为另一个视图。 To do that, you should first set the newView's tag .为此,您应该首先设置 newView 的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 .playAction中,获取newView并将其从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.同样可以实现play:方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM