gpt4 book ai didi

ios - 如何在 ios 中的 ViewController 上加载 .xib UIviews

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:25:01 24 4
gpt4 key购买 nike

嗨,我是 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com