如何在 ios 中的 ViewController 上加载 .xib UIview
How to load .xib UIviews on ViewController in ios
嗨,我是 ios 的新手,在我的应用程序中,我正在使用 .xib 文件加载 UIView
当我点击第一个按钮时,我想加载 FirstView 并删除其他视图
当我单击第二个按钮时,我想加载 SecondView 并删除其他视图
当我单击第三个按钮时,我想加载 ThirdView 并删除其他视图
我的代码:-
#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
1)
使视图全局化
FirstView * test1;
SecondView * test2;
ThirdView * test3;
随时从超级视图中删除:
[test1 removeFromSuperView];
2) 添加标签查看
test1.tag = 10;
使用标记值删除视图:
[(UIView*)[rightView viewWithTag:10] removeFromSuperview];
以下是您需要的方法。
/* 没有标签 */
- (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];
}
}
}
希望对您有所帮助。
试试这段代码:我已经为视图编写了代码,它将在添加新视图之前删除以前的视图。
#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
首先你在 NSBundle[=18 中创建 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];
}
使用此代码。它包含一个 loadXib:
调用,该调用使用给定名称和 returns 从笔尖加载视图。
@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
嗨,我是 ios 的新手,在我的应用程序中,我正在使用 .xib 文件加载 UIView
当我点击第一个按钮时,我想加载 FirstView 并删除其他视图 当我单击第二个按钮时,我想加载 SecondView 并删除其他视图 当我单击第三个按钮时,我想加载 ThirdView 并删除其他视图
我的代码:-
#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
1) 使视图全局化
FirstView * test1;
SecondView * test2;
ThirdView * test3;
随时从超级视图中删除:
[test1 removeFromSuperView];
2) 添加标签查看
test1.tag = 10;
使用标记值删除视图:
[(UIView*)[rightView viewWithTag:10] removeFromSuperview];
以下是您需要的方法。
/* 没有标签 */
- (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];
}
}
}
希望对您有所帮助。
试试这段代码:我已经为视图编写了代码,它将在添加新视图之前删除以前的视图。
#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
首先你在 NSBundle[=18 中创建 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];
}
使用此代码。它包含一个 loadXib:
调用,该调用使用给定名称和 returns 从笔尖加载视图。
@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