作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
嗨,我是 ios 的新手,在我的应用程序中,我正在使用 .xib 文件加载 UIView
当我点击第一个按钮时,我想加载 FirstView 并删除其他 View 当我单击第二个按钮时,我想加载 SecondView 并删除其他 View 当我单击第三个按钮时,我想加载 ThirdView 并删除其他 View
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize leftView,rightView;
- (void)viewDidLoad {
[super viewDidLoad];
}
- (IBAction)FirstAction:(id)sender {
FirstView * test1 = [[FirstView alloc]initWithFrame:CGRectMake(0, 0, rightView.frame.size.width, rightView.frame.size.height)];
test1.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
[rightView addSubview:test1];
}
- (IBAction)SecondAction:(id)sender {
SecondView * test2 = [[SecondView alloc]initWithFrame:CGRectMake(0, 0, rightView.frame.size.width, rightView.frame.size.height)];
test2.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
[rightView addSubview:test2];
}
- (IBAction)ThirdAction:(id)sender {
ThirdView * test3 = [[ThirdView alloc]initWithFrame:CGRectMake(0, 0, rightView.frame.size.width, rightView.frame.size.height)];
test3.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
[rightView addSubview:test3];
}
@end
最佳答案
试试这段代码:我已经为 View 编写了代码,它将在添加新 View 之前删除以前的 View 。
#import "ViewController.h"
@interface ViewController ()
{
FirstView * test1;
SecondView * test2;
ThirdView * test3;
}
@end
@implementation ViewController
@synthesize leftView,rightView;
- (void)viewDidLoad {
[super viewDidLoad];
test1 = [[[NSBundle mainBundle] loadNibNamed:@"FirstView" owner:self options:nil] objectAtIndex:0];
test2 = [[[NSBundle mainBundle] loadNibNamed:@"SecondView" owner:self options:nil] objectAtIndex:0];
test3 = [[[NSBundle mainBundle] loadNibNamed:@"ThirdView" owner:self options:nil] objectAtIndex:0];
}
- (IBAction)FirstAction:(id)sender {
test1.frame = CGRectMake(0, 0, rightView.frame.size.width, rightView.frame.size.height);
test1.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
[self removePreviousView:test1 FromSuperView:rightView];
[rightView addSubview:test1];
}
- (IBAction)SecondAction:(id)sender {
test2.frame = CGRectMake(0, 0, rightView.frame.size.width, rightView.frame.size.height);
test2.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
[self removePreviousView:test2 FromSuperView:rightView];
[rightView addSubview:test2];
}
- (IBAction)ThirdAction:(id)sender {
test3.frame = CGRectMake(0, 0, rightView.frame.size.width, rightView.frame.size.height);
test3.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
[self removePreviousView:test3 FromSuperView:rightView];
[rightView addSubview:test3];
}
- (void)removePreviousView:(UIView*)previousView FromSuperView:(UIView*)view{
for (UIView *subView in view.subviews) {
if (![subView isKindOfClass:[previousView class]]) {
[subView removeFromSuperview];
}
}
}
@end
关于ios - 如何在 ios 中的 ViewController 上加载 .xib UIviews,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35405053/
我是一名优秀的程序员,十分优秀!