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

Posted

技术标签:

【中文标题】如何在 iOS 中的 ViewController 上加载 .xib UIviews【英文标题】:How to load .xib UIviews on ViewController in ios 【发布时间】:2016-02-15 08:57:17 【问题描述】:

您好,我是 ios 新手,在我的应用中,我正在使用 .xib 文件加载 UIView

当我单击第一个按钮时,我想加载 FirstView 并删除其他视图 当我单击第二个按钮时,我想加载 SecondView 并删除其他视图 当我单击第三个按钮时,我想加载 ThirdView 并删除 otherviews

我的代码:-

#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

【问题讨论】:

试试我提供的解决方案 您说您正在使用 xib 文件加载视图。但是您正在您的操作方法中以编程方式创建它们。你的项目中有 xib 文件吗? 是的,我的项目中有 xib 文件,这是我的示例应用程序 drive.google.com/file/d/0B05NQoG9RAmwd2NnU3AzVlRzdjg/… 【参考方案1】:

试试这段代码:我已经为视图编写了代码,它将在添加新视图之前删除以前的视图。

#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

【讨论】:

如果有错误请修改好吗兄弟? 你检查了吗?兄弟 是的,我已经检查过您以错误的方式做几件事。就像您在 nib 中设置了视图的类,但您没有从 nib 加载视图。 如果你修改了,你能把那个示例项目发给我吗? 我已经更新了代码。将其替换为 UIViewController 中的代码并查看结果。【参考方案2】:

1) 使视图全局化

FirstView * test1;
SecondView * test2;
ThirdView * test3;

随时从超级视图中删除:

[test1 removeFromSuperView];

2)添加标签查看

test1.tag = 10;

使用标签值移除视图:

[(UIView*)[rightView  viewWithTag:10] removeFromSuperview];

【讨论】:

抱歉sn-p中的任何类型。 嗨 @KDeogharkar 没有添加 xib UIview 所以你是说视图没有显示? 第一、二、三视图有xib? 让我们continue this discussion in chat。【参考方案3】:

使用此代码。它包含一个 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

【讨论】:

【参考方案4】:

以下是您需要的方法。

/* 不带标签 */

- (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];

        
    


希望对你有所帮助。

【讨论】:

【参考方案5】:

首先你在NSBundle中创建UiViewIBOutlets然后你选择这个方法

- (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];


【讨论】:

以上是关于如何在 iOS 中的 ViewController 上加载 .xib UIviews的主要内容,如果未能解决你的问题,请参考以下文章

如何从 iOS 中的 NSObject 类导航到 ViewController?

如何根据 iOS 中的两个条件从 AppDelegate 呈现两个不同的 ViewController?

如何将 Firebase iOS 中的数据从 TableViewController 传递到另一个 ViewController

ios 7中的UI Popover ViewController?

iOS 将storybord中的视图与viewcontroller相关联

将 ViewController 嵌入 Storyboard 中的另一个 ViewController