【问题标题】:Change the view of a UIViewController dynamically in iOS 7在 iOS 7 中动态更改 UIViewController 的视图
【发布时间】:2014-01-14 04:12:41
【问题描述】:

我有 2 个自定义视图类 (CustomView_A, CustomView_B) 派生自 UIView。我有 UIViewController 应该能够在运行时在视图之间切换..

到目前为止,我所做的是.. 在 Storyboard 中,我使用 CustomView_A 类作为 View 类。

@interface MyViewController: UIViewController

@property (nonatomic, weak) CustomView_A *customView_A;

现在我有了第二个CustomView_B 类,我想在运行时将MyViewController 的视图更改为CustomView_B

我该怎么做?提前谢谢..

【问题讨论】:

  • 你可以简单地隐藏 CustomView_A 并使用 UIViewAnimation 显示 CustomView_B!
  • 您的 CustomView_A/B 在 Nib/storyboard/Code 中?它很容易设置你self.view = CustomView_B instance

标签: ios objective-c uiview uiviewcontroller ios7


【解决方案1】:

好的,这是你想要的代码 -

在你的MyViewController.h 放 -

@property (nonatomic, retain) CustomView_A *customView_A;
@property (nonatomic, retain) CustomView_B *customView_B;

-(void)switchView; // to switch the views.

在你的MyViewController.m 放 -

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.customView_A = [[CustomView_A alloc]initWithFrame:self.view.frame];
    self.customView_A.backgroundColor = [UIColor redColor];

    UIButton *trigger = [UIButton buttonWithType:UIButtonTypeRoundedRect]; // Just take this button so that your switchView methods will get called on click of this method.
    [trigger setFrame:CGRectMake(10, 10, 50, 30)];
    [trigger setTitle:@"Click" forState:UIControlStateNormal];
    [trigger addTarget:self action:@selector(switchView) forControlEvents:UIControlEventTouchUpInside];
    [self.customView_A addSubview:trigger];

    [self.view addSubview:self.customView_A];

    self.customView_B = [[CustomView_B alloc]initWithFrame:self.view.frame];
    self.customView_B.backgroundColor = [UIColor yellowColor];
    self.customView_B.hidden = YES;
    [self.view addSubview:self.customView_B];
}

- (void)switchView
{

    [UIView animateWithDuration:10 delay:10 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        self.customView_A.hidden = YES;
        self.customView_B.hidden = NO;
    } completion:nil];

}

当你再次想切换视图时,做相反的事情。

【讨论】:

    【解决方案2】:

    不要。您所描述的是对 UIViewController 的根本误解。一旦一个 UIViewController 实例有一个view,那就永远是它的view

    如果您想要两个不同的视图,那么:

    • 使用两个视图控制器(例如,您可以在视图控制器 A 及其视图之上呈现视图控制器 B 及其视图,使用模态 segue),或

    • 使这些视图中的至少一个不属于视图控制器:只需将该视图放在另一个视图的前面,然后再将其删除,随意。

    【讨论】:

      【解决方案3】:

      试试这个:

      - (IBAction)changeView {
             if (self.customView_A.hidden == YES) {
                  self.customView_A.hidden = NO;
                  self.customView_B.hidden = YES;
                  //You should use a UIView animation here to do this.
             }
             else {
                  self.customView_A.hidden = YES;
                  self.customView_B.hidden = NO;
                  //Same here
             )
      }
      

      在您的viewDidLoad 中将视图添加到CGRectZero

      - (void)viewDidLoad {
             self.customView_A = [[CustomView_A alloc]initWithFrame:CGRectZero];
             [self.view addSubview:self.customView_A];
             //do the same with the other custom view
      }
      

      对不起,如果代码有点错误,我没有使用Xcode 来输入。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-09-30
        • 2016-04-02
        相关资源
        最近更新 更多