在iOS 7中动态更改UIViewController的视图

在iOS 7中动态更改UIViewController的视图,ios,objective-c,uiview,uiviewcontroller,ios7,Ios,Objective C,Uiview,Uiviewcontroller,Ios7,我有两个自定义视图类(CustomView\u A,CustomView\u B)是从UIView派生的。我有UIViewController,它应该能够在运行时在视图之间切换 到目前为止,我所做的是。。在故事板中,我使用CustomView\u A类作为视图类 @interface MyViewController: UIViewController @property (nonatomic, weak) CustomView_A *customView_A; 现在我有了第二个Custom

我有两个自定义视图类(
CustomView\u A,CustomView\u B
)是从
UIView
派生的。我有
UIViewController
,它应该能够在运行时在视图之间切换

到目前为止,我所做的是。。在故事板中,我使用
CustomView\u A
类作为视图类

@interface MyViewController: UIViewController

@property (nonatomic, weak) CustomView_A *customView_A;
现在我有了第二个
CustomView\u B
类,我想在运行时将
MyViewController
的视图更改为
CustomView\u B

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

试试这个:

- (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
来键入此代码。

请尝试以下操作:

- (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
来键入此代码。

不要。您所描述的是对UIViewController的一个基本误解。一旦UIViewController实例拥有一个
视图
,这就是它的
视图

如果需要两个不同的视图,则可以选择:

  • 使用两个视图控制器(例如,可以使用模式序列在视图控制器A及其视图的顶部显示视图控制器B及其视图),或

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


    • 不要这样做。您所描述的是对UIViewController的一个基本误解。一旦UIViewController实例拥有一个
      视图
      ,这就是它的
      视图

      如果需要两个不同的视图,则可以选择:

      • 使用两个视图控制器(例如,可以使用模式序列在视图控制器A及其视图的顶部显示视图控制器B及其视图),或

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


        • 好的,这是您想要的代码-

          在您的
          MyViewController.h
          put-

          @property (nonatomic, retain) CustomView_A *customView_A;
          @property (nonatomic, retain) CustomView_B *customView_B;
          
          -(void)switchView; // to switch the views.
          
          - (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];
          
          }
          
          在您的
          MyViewController.m
          put-

          @property (nonatomic, retain) CustomView_A *customView_A;
          @property (nonatomic, retain) CustomView_B *customView_B;
          
          -(void)switchView; // to switch the views.
          
          - (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];
          
          }
          

          再次切换视图时,请执行相反的操作。

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

          在您的
          MyViewController.h
          put-

          @property (nonatomic, retain) CustomView_A *customView_A;
          @property (nonatomic, retain) CustomView_B *customView_B;
          
          -(void)switchView; // to switch the views.
          
          - (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];
          
          }
          
          在您的
          MyViewController.m
          put-

          @property (nonatomic, retain) CustomView_A *customView_A;
          @property (nonatomic, retain) CustomView_B *customView_B;
          
          -(void)switchView; // to switch the views.
          
          - (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];
          
          }
          

          再次切换视图时,请执行相反的操作。

          u只需使用UIViewAnimation隐藏CustomView\u A并显示CustomView\u B即可!您的CustomView\u A/B是否在Nib/情节提要/代码中?只需将您设置为
          self.view=CustomView\u B实例
          ,您就可以简单地隐藏CustomView\u A并使用UIViewAnimation显示CustomView\u B!您的CustomView\u A/B是否在Nib/情节提要/代码中?只需将您设置为
          self.view=CustomView\u B实例
          ,就很容易了。