代码之家  ›  专栏  ›  技术社区  ›  AbhiRam

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

  •  2
  • AbhiRam  · 技术社区  · 9 年前

    嗨,我对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
    
    5 回复  |  直到 9 年前
        1
  •  3
  •   Shehzad Ali    9 年前

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

    #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
    
        2
  •  1
  •   KDeogharkar    9 年前

    1) 使视图全局

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

    随时从超级视图中删除:

    [test1 removeFromSuperView];
    

    2) 将标记添加到视图

    test1.tag = 10;
    

    使用标记值删除视图:

    [(UIView*)[rightView  viewWithTag:10] removeFromSuperview];
    
        3
  •  1
  •   Akshay Sunderwani    9 年前

    使用此代码。它包含一个 loadXib: 调用,从具有给定名称的笔尖加载视图并返回该视图。

    @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
  •  0
  •   nikhil84    9 年前

    下面是您需要的方法。

    /*无标签*/

    - (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
  •  0
  •   Sreekanth    9 年前

    首先创建 UiView IB出口 NSBundle软件包 然后你选择这个方法

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