我有 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/