首页 文章

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

提问于
浏览
0

我有点堆叠创建两个可切换的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";
 }

但只有正常工作的是“ Headers ”,并且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 ..它在导航视图中,在选择一个单元格之后,这个带有两个UIVies的UIVieController(percView)被分配

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

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

2 回答

  • 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

    示例代码;

    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];
     }
    

相关问题