ios中xib的使用介绍

ios中Xib的使用

ios中xib的使用

Nib files are the quintessential(典型的) resource type used to create iOS and Mac apps. A nib file is a data archive that essentially contains a set of freeze-dried objects that you want to recreate at runtime. Nib files are used most commonly to store preconfigured windows, views, and other visually oriented objects but they can also store nonvisual(不可见的) objects such as controllers.

You edit nib files in Xcode with Interface Builder, which provides a graphical editor for assembling your objects. When you subsequently load a nib file into your application, the nib-loading code instantiates each object in the file and restores it to the state you specified in Interface Builder. Thus, what you see in Interface Builder is really what you get in your application at runtime.

也就是说,nib文件(就是xib文件)是描述应用外观的一个描述文档,这里面包含了用户界面和用户界面相关元素。这样的话,可以方便我们编写程序。

在xcode中,以“sourece code”方式打开xib文件,发现他是一份以xml编码的文件,也就是说xib文件的本质是一份xml的脚本,描述了程序员对文件中的view及其子类等的设置,通过这样的方式来连接xib和代码。如下图所示,左边为xib正常打开方式,右边是xib文件使用sourece code打开。

                  

下面直接切入正题,介绍创建一个xib方法:

A:介绍最基本的xib文件的创建和使用

一:单个view的提取和使用

1.创建一个xib文件,然后设置这个xib文件中的属性,设置如下:

a:file’s  owner的类不需要设置,默认为nsobject类

b:在xib中添加一个view1,然后设置view的属性如下:


(这样,我们就可以改变view的大小了)

c:改变view的颜色。(方便我们观察)


2.在需要使用view1的类文件中添加下面的代码:

NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"testXib" owner:nil options:nil];
    UIView *view = [array firstObject];
    [self.view addSubview:view];

知识点:

1.通过[[NSBundle mainBundle] loadNibNamed: owner: options:];方法来获取了xib中的属性。

2.通过[array firstObject]方法,提取出这个数组中的实例对象

效果图:

二:在xib中创建两个view

1.通过和上面相同的方法来为一个xib中创建两个view1和view2.


2.需要在使用view1和view2的类文件中添加下面的代码:

NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"testXib" owner:nil options:nil];
    UIView *view1 = [array firstObject];
    UIView *view2 = [array objectAtIndex:1];
    [self.view addSubview:view1];
    [self.view addSubview:view2];

知识点:

1.xib文件中,我们可以设置多个view。

2.xib文件其实就是一个自定义view的工具,我们可以在xib中定义多个属性,然后再使用代码提取出这些属性。

3.代码中的array里面存放的其实就是xib文件中创建的所有的属性。

效果图:


B:view直接与xib进行关联

一:类中创建的view1属性与xib中的创建的view进行关联


注:用viewController类与xib进行关联

1.首先在ViewController类中创建熟悉:

#import "ViewController.h"

@interface ViewController ()
@property (nonatomic, strong) IBOutlet UIView *view1;
@end

2.创建一个xib文件,把xib文件中的file owner的类改成ViewController:


3.在这个xib中创建一个UIView(与刚刚方法相同)

4.打开file owner中的关联属性栏,你会发现这边有一个view1(这个熟悉就是我们刚刚创建的属性),我们将view1和xib中的创建的view关联起来


5.在ViewController类中添加代码:

[[NSBundle mainBundle] loadNibNamed:@"testXib" owner:self options:nil];
    [self.view addSubview:self.view1];

知识点:

1.将file owner的类变成ViewController这个类,就相当于说明了这个xib文件是属于ViewController类。

2.我们将view1和xib中创建的新的view关联起来就相当于将他们属性与属性描述链接起来,在代码运行中会自动对view1进行相应要求的创建。

3.要实现2中的效果,我们需要使用[[NSBundlemainBundle] loadNibNamed:@"testXib"owner:selfoptions:nil];

方法,并将owner改为self。因为这个xib现在是属于viewControlelr这个类的。

效果图:



二:ViewController类中创建view1,view2,并于xib进行关联

1.在ViewController类中创建两个view:

#import "ViewController.h"

@interface ViewController ()
@property (nonatomic, strong) IBOutlet UIView *view1;
@property (nonatomic, strong) IBOutlet UIView *view2;
@property (nonatomic, strong) IBOutlet UILabel *labletest;

@end

2.在xib中将file owner的类改为viewController

3.在xib中创建两个view:

如图:


4.打开file owner中的关联属性界面,我们可以看到:


这边显示了我们刚刚在代码中创建的三个属性。我们将view1和view2关联起来。

5.在viewController中添加代码:

[[NSBundle mainBundle] loadNibNamed:@"testXib" owner:self options:nil];
    [self.view addSubview:self.view1];
    [self.view addSubview:self.view2];

知识点:

1.将xib的file owner改为ViewController。那么在xib中的属性关联界面中,会出现相关的属性。通过调整xib的file owner,就是说这里面存在一定的关联。

效果图:


补充:一个类可以关联多个xib文件。还有如果两个xib文件中都关联了这个类中的同一个view,那是不会报错。但是如果一开始先加载了一个xib文件,后来有加载了另一个xib文件,然后这两个xib中都关联了同一个view,那么关于这个view的设置会被覆盖掉。


C:创建一个XibManager的类,来管理xib文件。

1.首先创建一个NSObject的子类,名字为XibManager。然后在这个类中添加两个view1,view2.并在实现文件中重写init方法。

代码为:

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

@interface XibNamager : NSObject
@property (nonatomic ,strong) IBOutlet UIView *view1;
@property (nonatomic, strong) IBOutlet UIView *view2;

@end

#import "XibNamager.h"

@implementation XibNamager

-(id) init{
    [[NSBundle mainBundle ] loadNibNamed:@"testXib" owner:self options:nil];
    return self;
}
@end

2.创建一个xib文件,然后将file owner类改为XibManager类。

3.在xib中创建两个view,然后与XibManager关联起来。

图11

4.在viewController中导入XibManager类

5.在viewController中编写代码:

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    XibNamager *manager = [[XibNamager alloc] init];
    [self.view addSubview:manager.view1];
    [self.view addSubview:manager.view2];
}

知识点:在这边创建了一个XibManager,其实就是使得代码更加简单,声明了一个类来统一管理xib中的view。

效果图为:


d:用在xib中创建一个button,然后实现button,将这个xib视图加载到主视图中。

一.使用UIView来写button的action方法

1.创建一个Xib文件,然后在xib中出创建view1,在view1中设置button如下。
2.创建一个UIView类型的子类XibView,类中的代码如下:
#import <UIKit/UIKit.h>

@interface Xibview : UIView

-(IBAction)buttonAction:(id)sender;

@end

#import "Xibview.h"

@implementation Xibview

-(IBAction)buttonAction:(id)sender{
    NSLog(@"UIView中的button");
}
@end
3.将这个xib设置的view的类改为XibView类,然后将这个view中的button和代码中的buttonAction方法关联起来。
4.最后,和上面一样,我们在viewController类中加入这个自定义的类。
下面代码:
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    
    NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"Xibview" owner:nil options:nil];
    UIView *view2 = [array firstObject];
    CGRect frame2 = [view2 frame];
    frame2.origin.y = 100;
    [view2 setFrame:frame2];
    [self.view addSubview:view2];
}
效果图:


二.使用UIViewController来写button的action方法

1.创建xib文件,如下图所示:

2.创建一个继承了UIViewController的子类,名为xibViewController。代码如下:
#import <UIKit/UIKit.h>

@interface xibViewController : UIViewController

- (IBAction)buttonAction:(id)sender;
@end

#import "xibViewController.h"

@interface xibViewController ()

@end

@implementation xibViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (IBAction)buttonAction:(id)sender {
    NSLog(@"点按方法");
}
@end
3.将xib中file owner的class改为xibViewController,然后将button和xibViewController中的方法关联起来。
4.在viewcontroler类中加在view,然后点击按钮。
这里我们需要注意下:我们需要创建一个下面类型的controller,因为如果我们不创建这么一个属性,我们在调用界面的时候,调用界面完成了之后,controller就会自动释放,这就导致了我们点击button方法的时候,button方法获取不到,会报错:
@interface ViewController ()

@property(nonatomic,strong)xibViewController *controller;

@end

下面是view的获取代码:
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    self.controller = [[xibViewController alloc] initWithNibName:@"xibViewController" bundle:nil];
    UIView *view1 = self.controller.view;
    CGRect frame1 = [view1 frame];
    frame1.origin.y = 20;
    [view1 setFrame:frame1];
    [self.view addSubview:self.controller.view];

}

效果图:



三.使用NSObject来写button的action方法

注意:这边我们需要了解到一个问题,就是xib中的view,或者button都是可以在NSobject中编写的。就是说,我们编写图形化的控件,没有一定的要求,只要是导入了

#import <UIKit/UIKit.h>这个类文件的地方,我们都可以编写可视化控件。然后我们也可以编写button的action方法。我们将xib中的文件编写在一个NSObject的子类里面,可以使得代码更加清晰,思路更加明了。

下面是操作方法:

1.创建一个xib,然后在这个xib中添加view1,在view1上添加button按钮

2.创建NSObject的子类xibFileManager,然后在文件中添加下面代码:

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

@interface xibFile : NSObject

@property (nonatomic, strong) IBOutlet UIView *view1;

-(IBAction)buttonAction:(UIButton*)button;

@end

#import "xibFile.h"

@implementation xibFile

-(id) init{
    self = [super init];
    [[NSBundle mainBundle] loadNibNamed:@"xibFile" owner:self options:nil];
    return self;
}

-(IBAction)buttonAction:(UIButton*)button{
    NSLog(@"在file中创建了点击事件");
}

@end

3.更改xib文件中的file owner的class名称为xibfile,并且关联相关属性和方法。

4.在viewcontroller中添加下面代码:

这边我们同样需要声明一个fileManager变量。防止arc提前释放fileManager导致view中button方法获取不到。

@interface ViewController ()
@property (nonatomic, strong) xibFile *fileManager;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.fileManager = [[xibFile alloc] init];
    [self.view addSubview:self.fileManager.view1];
}














原文地址:https://www.cnblogs.com/AbeDay/p/5026943.html