您好我是ios的新手,在我的应用中我使用.xib文件加载UIView
当我点击第一个按钮时,我想加载FirstView并删除其他视图 当我点击第二个按钮我想加载SecondView并删除其他视图 当我点击第三个按钮我想加载ThirdView并删除其他视图
RadGridView radGridView1 = new RadGridView();
this.Controls.Add(radGridView1);
radGridView1.Dock = DockStyle.Fill;
DataTable table = new DataTable();
table.Columns.Add("col1");
table.Columns.Add("col2");
table.Rows.Add("value1", "value1");
table.Rows.Add("value2", "value2");
radGridView1.DataSource = table;
答案 0 :(得分:3)
尝试这段代码:我编写了视图代码,在添加新代码之前删除了以前的视图。
#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
答案 1 :(得分:1)
<强> 1)强> 使视图全局
FirstView * test1;
SecondView * test2;
ThirdView * test3;
随时从superview中删除:
[test1 removeFromSuperView];
2)添加标记到视图
test1.tag = 10;
使用标记值删除视图:
[(UIView*)[rightView viewWithTag:10] removeFromSuperview];
答案 2 :(得分:1)
使用此代码。它包含一个loadXib:
调用,它使用给定的名称从nib加载视图并返回它。
@interface ViewController ()
{
FirstView * test1;
SecondView * test2;
ThirdView * test3;
}
@end
@implementation ViewController
@synthesize leftView,rightView;
- (void)viewDidLoad {
[super viewDidLoad];
}
-(UIView*)loadXib:(NSString *)name
{
UINib *nib = [UINib nibWithNibName:name bundle:nil];
if (nib != nil)
{
NSArray *items = [nib instantiateWithOwner:self options:nil];
if (items != nil && items.count == 1)
{
return (UIView*)items[0];
}
}
return nil;
}
- (IBAction)FirstAction:(id)sender {
// test1 = [[FirstView alloc]initWithFrame:CGRectMake(0, 0, rightView.frame.size.width, rightView.frame.size.height)];
test1 = (FirstView*)[self loadXib:@"FirstView"];
if (test1 != nil) {
test1.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
[rightView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
[rightView addSubview:test1];
}
}
- (IBAction)SecondAction:(id)sender {
// test2 = [[SecondView alloc]initWithFrame:CGRectMake(0, 0, rightView.frame.size.width, rightView.frame.size.height)];
test2 = (SecondView*)[self loadXib:@"SecondView"];
if (test2 != nil) {
test2.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
[rightView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
[rightView addSubview:test2];
}
}
- (IBAction)ThirdAction:(id)sender {
// test3 = [[ThirdView alloc]initWithFrame:CGRectMake(0, 0, rightView.frame.size.width, rightView.frame.size.height)];
test3 = (ThirdView*)[self loadXib:@"ThirdView"];
if (test3 != nil ) {
test3.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
[rightView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
[rightView addSubview:test3];
}
}
@end
答案 3 :(得分:0)
以下是您需要的方法。
/ *没有标签* /
- (void)removeSubviewsExpectView:(id)viewClass {
for (UIView *subView in self.view.subviews) {
if (![subView isKindOfClass:[viewClass class]]) {
[subView removeFromSuperview];
}
}
}
/ *带标签* /
- (void)removeSubviewsExpectView:(int)viewTag {
for (UIView *subView in self.view.subviews) {
if (subView.tag != viewTag) {
[subView removeFromSuperview];
}
}
}
希望这可以帮助你。
答案 4 :(得分:0)
首先,您在 NSBundle 中创建UiView
IBOutlets ,然后选择此方法
- (IBAction)FirstAction:(id)sender {
NSArray *viewsToRemove = [rightView subviews];
for (UIView *v in viewsToRemove) {
[v removeFromSuperview];
}
UIView *firstViewUIView = [[[NSBundle mainBundle] loadNibNamed:@"Test1" owner:self options:nil] firstObject];
[rightView containerView addSubview:firstViewUIView];
}
- (IBAction)SecondAction:(id)sender {
NSArray *viewsToRemove = [rightView subviews];
for (UIView *v in viewsToRemove) {
[v removeFromSuperview];
}
UIView *secondView = [[[NSBundle mainBundle] loadNibNamed:@"Test2" owner:self options:nil] firstObject];
[rightView containerView addSubview:seconView];
}
- (IBAction)ThirdAction:(id)sender {
NSArray *viewsToRemove = [rightView subviews];
for (UIView *v in viewsToRemove) {
[v removeFromSuperview];
}
UIView *thirdView = [[[NSBundle mainBundle] loadNibNamed:@"Test3" owner:self options:nil] firstObject];
[rightView containerView addSubview:thirdView];
}