2010-11-10 62 views
0

我是创建两个可切换的UIViews(每个UIView有一些子视图)的位堆栈。由于我不是IB,我想以编程的方式来做。1 UIViewController和2以编程方式切换UIViews

我已经尝试将我的子视图添加到我的第一个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..it是在导航视图和带有两个UIVies选择一个小区此UIVieController(percView)之后被分配

 
    ShakeControl *percView = [[ShakeControl alloc] init]; 
    [self.navigationController pushViewController:percView animated:YES]; 
    [percView release]; 

EDIT3 开关由按钮来完成,但细做,因为我使用了很多次,它的工作

回答

3

这里是你想要做什么视图控制器的一个实例(我觉得) 。这是非常基本的,只是在具有不同颜色背景的两个视图之间切换。但是,您可以在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 
+0

这是我原本认为有效的代码,但没有。 – Vanya 2010-11-10 16:08:14

+0

你是否通过重写loadView来初始化self.view?假设self.view不是零,它的框架大小和位置是否正确? – 2010-11-10 16:16:17

+0

是的,我重写loadView和self.view有应用程序框架大小的视图 – Vanya 2010-11-10 16:24:46

0

样本代码;


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]; 
} 
相关问题