我可以动态更改UIView的内容吗?

时间:2012-10-31 09:24:00

标签: iphone objective-c ios cocoa

我想在不使用removeFromSuperView和addSubview的情况下动态更改UIView的内容。代码是我想要做的图像。

int screenWidth = [[UIScreen mainScreen] bounds].size.width;
int screenHeight = [[UIScreen mainScreen]bounds].size.height;
UIView *currentView = [[UIView alloc]initWithFrame:CGRectMake(0,0,screenWidth,screenHeight)];
UIView *nextView = [[UIView alloc]initWithFrame:CGRectMake(0,0,screenWidth,screenHeight)];

[self.view addSubview:currentView];

currentView = nextView;

//when this code run,appearing currentView swap nextView.
[self.view reload];

2 个答案:

答案 0 :(得分:1)

当然。如果您希望它看起来不错,请使用您想要的属性创建“结束”视图,并使用静态transition方法:+transitionFromView:toView:duration:options:completion:

这将根据需要删除和添加视图,这样您就不必调用这些方法。否则,您可以使用 janusfidel 建议的-setHidden:方法,以隐藏您不想看到的视图。

答案 1 :(得分:0)

//in viewdidload
{
........
........
[self.view addSubview:currentView];
[self.view addSubview:nextView];
nextView .hidden = YES;
}

-(void)changeView
{
if(!currentView .hidden)
{
currentView .hidden =YES;
nextView .hidden = NO;
}
else
{
currentView .hidden =NO;
nextView .hidden = YES;
}
}