1个UIViewController和2个编程切换UIViews
我与创建两个可切换的UIViews(每个UIView有一些子视图)位堆栈。 由于我不是IB,我想以编程的方式来做。
我曾尝试将我的子视图添加到我的第一个UIView,然后与第二个相同,然后切换到这样的视图。
if ([self.setView superview]) { [self.setView removeFromSuperview]; [self.view addSubview:contentView]; self.navigationItem.title = @"BaseLine"; } else { [self.contentView removeFromSuperview]; self.view = setView; self.navigationItem.title = @"Settings"; }
但只有正确工作的是“标题”,没有出现在UIViews上。 UIView似乎是好的,因为当我使用
self.view = contentView; or self.view = setView;
都正确显示子视图。
请有人踢我到这个正确的方向。
TNX
编辑:同时指出解决方案,可能是我在loadView中的初始化错误
CGRect screenRect = [[UIScreen mainScreen] applicationFrame]; contentView = [[UIView alloc] initWithFrame:screenRect]; setView = [[UIView alloc] initWithFrame:screenRect]; self.view = contentView;
我试图首先添加它[自我。 查看添加..]但应用程序崩溃,所以我用这个
EDIT2
根控制器是UITableViewController ..它是在导航视图中,并选择一个单元格后,这个UIVieController(percView)与两个UIVies分配
ShakeControl *percView = [[ShakeControl alloc] init]; [self.navigationController pushViewController:percView animated:YES]; [percView release];
EDIT3开关完成按钮,但它很好,因为我用了很多次,它的工作
这是一个视图控制器的例子,它可以做你想做的事(我认为)。 这是非常基本的,只是在具有不同颜色背景的两个视图之间切换。 但是,您可以在loadView方法中进一步自定义这些子视图,以显示您需要的任何内容。
接口文件:
@interface SwapViewController : UIViewController {
UIView *firstView;
UIView *secondView;
BOOL displayingFirstView;
UIButton *swapButton;
}
@property (nonatomic, retain) UIView *firstView;
@property (nonatomic, retain) UIView *secondView;
@property (nonatomic, retain) UIButton *swapButton;
- (void)swap;
@end
执行文件:
#import "SwapViewController.h"
@implementation SwapViewController
@synthesize firstView;
@synthesize secondView;
@synthesize swapButton;
- (void)loadView
{
CGRect frame = [[UIScreen mainScreen] applicationFrame];
UIView *containerView = [[UIView alloc] initWithFrame:frame];
containerView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.view = containerView;
[containerView release];
frame = self.view.bounds;
firstView = [[UIView alloc] initWithFrame:frame];
firstView.backgroundColor = [UIColor redColor];
secondView = [[UIView alloc] initWithFrame:frame];
secondView.backgroundColor = [UIColor blueColor];
self.swapButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
self.swapButton.frame = CGRectMake(10, 10, 100, 50);
[swapButton addTarget:self action:@selector(swap) forControlEvents:UIControlEventTouchUpInside];
displayingFirstView = YES;
[self.view addSubview:firstView];
[self.view addSubview:swapButton];
}
- (void)swap
{
if(displayingFirstView) {
[self.firstView removeFromSuperview];
[self.view addSubview:secondView];
displayingFirstView = NO;
} else {
[self.secondView removeFromSuperview];
[self.view addSubview:firstView];
displayingFirstView = YES;
}
[self.view bringSubviewToFront:self.swapButton];
}
@end
示例代码;
BOOL firstview = YES; // First, I have added view1 as a subview;
if (firstview) {
[view1 removeFromSuperview];
[self.view addSubview:view2];
} else {
[view2 removeFromSuperview];
self.view addSubview:view1];
}
链接地址: http://www.djcxy.com/p/84301.html
上一篇: 1 UIViewController and 2 switching UIViews programmatically