0. 网络含有很多有关iOS封装framework .a静态库文章,本文推荐
1. 总结
bundle中存放图片,xib,storyboard等资源文件
### 获取NSBundle方法有2种
法一:
#define Bundle_IN_Framework [NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:@"你的bundle的名字" ofType:@"bundle"]]
法二:
#define Bundle_IN_Framework [NSBundle bundleWithPath:[[NSBundle bundleForClass:[self class]].resourcePath stringByAppendingPathComponent:@"/封装的bundle的名字.bundle"]]
【Storyboard】
#define StoryBoard_Name(@"你使用的storyboard名字") [UIStoryboard storyboardWithName:@"你使用的storyboard名字" bundle: Bundle_IN_Framework];
【图片】
#define ImageNamed(@"图片的名字") [UIImage imageNamed:@"图片的名字" inBundle: Bundle_IN_Framework compatibleWithTraitCollection:nil]
【xib】
#define Nib_Name(@"你的nib名字") [UINib nibWithNibName:@"你的nib名字" bundle: Bundle_IN_Framework]
使用
self.img.image = ImageNamed(@"图片名字");
viewController *vc = [StoryBoard_Name(@"Main") instantiateViewControllerWithIdentifier: "storybord中的ID"];
[tableView registerNib:Nib_Name(@"你的nib名字") forCellReuseIdentifier:@"复用Identifier"];
2.错误
- 在静态库中写了一个XXViewController类,然后在主工程的xib中,将xib的类指定为XXViewController,程序运行时,报了如下错误:
Unknown class XXViewController in Interface Builder file.
其实这个问题与Interface Builder无关,最直接的原因还是相关的symbol没有从静态库中加载进来。这种问题的处理就是在Target的“Build Setting
”–>“Other Link Flags
”中加上-all_load
和-ObjC
这两个标识位,这样就OK了。