ios - 在 iOS 7 中动态更改 UIViewController 的 View

标签 ios objective-c uiview uiviewcontroller ios7

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

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

@interface MyViewController: UIViewController

@property (nonatomic, weak) CustomView_A *customView_A;

现在我有第二个 CustomView_B我想更改 MyViewController 的 View 对 CustomView_B 的看法在运行时。

我怎样才能做到这一点?提前致谢..

最佳答案

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

在您的 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];

}

当您再次想要切换 View 时,请执行相反的操作。

关于ios - 在 iOS 7 中动态更改 UIViewController 的 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21105964/

相关文章:

ios - 目标 - C : UITableView Content Changing on Scroll

ios - UIImage用户信息

ios - 集成 FirebasePhoneAuthUI 进行电话号码身份验证

ios - 内部使用 self 的队列调用方法上的 weakSelf

ios - 当 View 已经被按下时如何响应触摸?

iphone - 如果父级处于纵向模式,如何在横向模式下加载 UIViewController?

objective-c - 执行 alloc 和 class_createInstance 有什么区别

ios - 如何使用 Swift 3 覆盖 UIView 中的 requiresConstraintBasedLayout?

iphone - 如何实现UIView的翻转过渡效果

ios - 使用土耳其语无点 i 进行字符串搜索