自己创建Cocoa touch FrameWork

1)按command+shift+n创建新工程 点击选择Framework & Library->Cocoa Touch Framework->next


给工程取名myFrameworkLibrary,点击next


2)选保存路径,点击create,会发现工程中包含了一个自动生成的头文件myFrameworkLibrary.h


3)按command+n创建文件依次选择Source->Cocoa Touch Class->next, 


取个类名myFrameworkManager,点击下一步,依次点击create


依次新建myViewController类以及myViewController.storyboard,设置如下


4)在myFrameworkManager

myFrameworkManager.h

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

@interface myFrameworkManager : NSObject

+(instancetype) sharedManager;

-(void) startManager;
-(void) stopManager;
<span style="font-family: Arial, Helvetica, sans-serif;">- (void) showViewCOntroller:(UIViewController *)viewController;</span>
-(BOOL) isManagerRunning;

@end

myFrameworkManager.m

#import "myFrameworkManager.h"
#import "myViewController.h"

@interface myFrameworkManager()

@property (nonatomic, assign) BOOL isEnabled;

@end

@implementation myFrameworkManager

+ (instancetype)sharedManager {
	static myFrameworkManager *shareManger = nil;
	static dispatch_once_t onceToken;
	dispatch_once(&onceToken, ^{
		shareManger = [[[self class] alloc] init];
	});
	
	return shareManger;
}

- (void)startManager {
	NSLog(@"Manager is running");
	_isEnabled = YES;
}

- (void)stopManager {
	NSLog(@"Manager stopped..");
	_isEnabled = NO;
}
-(BOOL) isManagerRunning {
	return _isEnabled;
}


- (void)showViewCOntroller:(UIViewController *)viewController {
	myViewController *vc = [[myViewController alloc] init];
	[viewController.navigationController pushViewController:vc animated:YES];
}

@end

myViewController.h

#import <UIKit/UIKit.h>

@interface myViewController : UIViewController

@end


myViewController.m

#import "myViewController.h"

@interface myViewController ()

@end

@implementation myViewController

- (instancetype)init {
	self = [UIStoryboard storyboardWithName:@"myViewController" bundle:[NSBundle bundleForClass:self.class]].instantiateInitialViewController;
	if (!self) {
		return nil;
	}
	
	return self;
}

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

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

5)依次点击工程myFrameworkLibrary->TARGETS->Build Phases ->Headers, 把myFrameworkManager.h从Project移到public,见图

结果如下


在myFrameworkLibrary.h头文件中包含myFrameworkManager.h文件

#import <UIKit/UIKit.h>
#import <myFrameworkLibrary/myFrameworkManager.h>

//! Project version number for myFrameworkLibrary.
FOUNDATION_EXPORT double myFrameworkLibraryVersionNumber;

//! Project version string for myFrameworkLibrary.
FOUNDATION_EXPORT const unsigned char myFrameworkLibraryVersionString[];

// In this header, you should import all the public headers of your framework using statements like #import <myFrameworkLibrary/PublicHeader.h>



myFrameworkManager


6)编译工程,选中整机设备或者Generic iOS Device,按command+b进行编译,在Products会看到myFrameworkLibrary.framework



7)鼠标右键,show in finder,便会看到已经编译好的framework库了



8)将上图中的库添加到新建的工程里,

新建工程名字为myFrameworkTest





将上一步中找到的myFrameworLibrary.framework拉到新建工程里,选中copy items if needed

9)appDeledate.m中的代码作如下更改

#import "AppDelegate.h"
#import "ViewController.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
	UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:[[ViewController alloc] init]];
	self.window.rootViewController = nav;
	return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application {
	// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
	// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
	// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
	// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
	// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
	// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
	// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end

Main.storyboard


ViewController.m 文件代码

#import "ViewController.h"
#import <myFrameworkLibrary/myFrameworkLibrary.h>

@interface ViewController ()

@end

@implementation ViewController
</pre><pre code_snippet_id="1570156" snippet_file_name="blog_20160129_11_1743679" name="code" class="objc"><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(255, 255, 255);">- (<span style="font-variant-ligatures: no-common-ligatures; color: #fb73ff">id</span>)init {</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(145, 188, 255);"><span style="font-variant-ligatures: no-common-ligatures; color: #ffffff"><span style="white-space:pre">	</span></span><span style="font-variant-ligatures: no-common-ligatures; color: #fb73ff">self</span><span style="font-variant-ligatures: no-common-ligatures; color: #ffffff"> = [</span><span style="font-variant-ligatures: no-common-ligatures; color: #8a93ff">UIStoryboard</span><span style="font-variant-ligatures: no-common-ligatures; color: #ffffff"> </span>storyboardWithName<span style="font-variant-ligatures: no-common-ligatures; color: #ffffff">:</span><span style="font-variant-ligatures: no-common-ligatures; color: #ff4d55">@"Main"</span><span style="font-variant-ligatures: no-common-ligatures; color: #ffffff"> </span>bundle<span style="font-variant-ligatures: no-common-ligatures; color: #ffffff">:</span><span style="font-variant-ligatures: no-common-ligatures; color: #fb73ff">nil</span><span style="font-variant-ligatures: no-common-ligatures; color: #ffffff">].</span>instantiateInitialViewController<span style="font-variant-ligatures: no-common-ligatures; color: #ffffff">;</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(251, 115, 255);"><span style="font-variant-ligatures: no-common-ligatures; color: #ffffff"><span style="white-space:pre">	</span></span>return<span style="font-variant-ligatures: no-common-ligatures; color: #ffffff"> </span>self<span style="font-variant-ligatures: no-common-ligatures; color: #ffffff">;</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(255, 255, 255);">}</p>

- (void)viewDidLoad {
	[super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
}
- (IBAction)clicked:(id)sender {
	
	[[myFrameworkManager sharedManager] showViewCOntroller:self];
	
}

- (void)didReceiveMemoryWarning {
	[super didReceiveMemoryWarning];
	// Dispose of any resources that can be recreated.
}

@end

在此进行操作,否则提示image not found


然后


运行,点击跳转就ok了。

注意事项:

需要外面用的累一定要移到public里面,并在头文件里引用

embed framework必须添加否则回提示image not found

当framework里有storyboard时,获取storyboard的参数bundle必须为当前的类


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

先行者-阿佰

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值