1个UIViewController和2个以编程方式切换UIViews

时间:2010-11-10 15:23:21

标签: iphone objective-c uiview uiviewcontroller

我有点堆叠创建两个可切换的UIViews(每个UIView都有一些子视图)。既然我没有IB,我想以编程方式做到这一点。

我尝试将我的子视图添加到我的第一个UIView,然后将其添加到第二个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;

我试图先添加它[self。查看添加..]但应用程序崩溃了,所以我使用了这个

EDIT2

根控制器是UITableViewController ..它在导航视图中,在选择了一个单元后,这个UIVieController(percView)被分配了两个UIVies

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

EDIT3 开关是通过按钮完成的,但它很好,因为我多次使用它并且它工作

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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];
 }