【问题标题】:1 UIViewController and 2 switching UIViews programmatically1 个 UIViewController 和 2 个以编程方式切换 UIView
【发布时间】:2011-05-07 22:55:06
【问题描述】:

我对创建两个可切换的 UIView 有点堆栈(每个 UIView 都有一些子视图)。因为我没有 IB,所以我想以编程方式做到这一点。

我尝试将我的子视图添加到我的第一个 UIView,然后与第二个相同,然后切换到这样的视图。

if ([self.setView superview]) { [self.setView removeFromSuperview]; [self.view addSubview:contentView]; self.navigationItem.title = @"BaseLine"; } 别的 { [self.contentView removeFromSuperview]; self.view = setView; self.navigationItem.title = @"设置"; }

但唯一能正常工作的是“标题”,UIViews 上什么也没有出现。 UIView 似乎还可以,因为当我使用时

self.view = contentView; 要么 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..它在导航视图中,在选择一个单元格后,这个带有两个 UIVies 的 UIVieController (percView) 被分配

ShakeControl *percView = [[ShakeControl alloc] init]; [self.navigationController pushViewController:percView 动画:YES]; [percView 发布];

EDIT3 开关是通过按钮完成的,但是做得很好,因为我使用了很多次并且它有效

【问题讨论】:

    标签: iphone objective-c uiview uiviewcontroller


    【解决方案1】:

    这是一个视图控制器的示例,它可以执行您想要的操作(我认为)。这是非常基本的,只是在具有不同颜色背景的两个视图之间切换。但是,您可以在 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
    

    【讨论】:

    • 这是我最初认为有效但无效的代码。
    • 你有没有通过重写 loadView 来初始化 self.view?假设 self.view 不为零,它的框架大小和位置是否正确?
    • 是的,我正在覆盖 loadView 并且 self.view 具有 applicationFrame 大小的视图
    • 这个视图控制器是如何显示的?它是根视图控制器吗?它在导航控制器中吗?另外,视图是如何切换的,一个 UIButton?你能发布更多的视图控制器类吗?
    • containerView 发布];导致该应用程序崩溃,但是当我删除版本时,这两个 UIView 分配混合在一起......伙计,我不认为这是可能的 :o),但我认为这是其他原因
    【解决方案2】:

    示例代码;

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-06-18
      • 2014-08-30
      • 1970-01-01
      • 2011-06-08
      • 2018-04-22
      • 2020-04-29
      • 1970-01-01
      相关资源
      最近更新 更多