作者sableangle (米米米米米奇)
看板MacDev
標題Re: [請益] 如何push Framework裡的viewcontroller
時間Sun Jan 20 04:01:52 2019
※ 引述《fbifxxkma (超棒Der)》之銘言:
: 我自製一個framework
: 裡面有一個viewcontroller.swift
: 還有他的storyboard 並設定他倆的對應關係
: 然後打包成framework
: ------
: 接著 我新開一個 project
: import 剛剛那個自製的 framework
: 我要如何push 出 framework裡面的畫面(viewcontroller)呢?
swift的我是不會啦
但是objective-c的我實作過 可以給你拋磚引玉一下
首先找到整個 App 的 UIWondow
大概像這樣,這樣你就可以在framework裡 去的 實際runtime時的UIWindow
UIWindow *keyWindow = [[[UIApplication sharedApplication] delegate] window];
然後從 Bundle 裡尋找需要的資源,大概像這樣
這個bundle你要自己包,要包的東西大概就圖片啊 Storyboard之類的
NSString* mainBundlePath = [[NSBundle mainBundle] resourcePath];
NSString* frameworkBundlePath = [mainBundlePath stringByAppendingPathComponent:@"YourBundle.bundle"];
接著你就可以從Bundle讀到你的StoryBoard了
yourViewController = [[UIStoryboard storyboardWithName:@"YourStoryBoardName" bundle:[NSBundle bundleWithPath:frameworkBundlePath]] instantiateViewControllerWithIdentifier:@"Nav"];
最後你就可以從UIWindow拿到 rootviewcontroller 有兩個 viewController基本上就可以為所欲為了
這是一個簡單的範例
[keyWindow.rootViewController.view addSubview: yourViewController.view];
大概是這樣啦
-----
Sent from JPTT on my iPhone
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 114.42.120.124
※ 文章網址: https://webptt.com/m.aspx?n=bbs/MacDev/M.1547928115.A.871.html
1F:→ sableangle: 手機發文 排版請見諒 01/20 04:03
2F:→ darktt: 不建議使用keyWindow去present畫面,因為有些UI元件會置 01/20 04:49
3F:→ darktt: 換原本的keyWindow,會導致階層不正確 01/20 04:49
4F:→ sableangle: 確實不是好方法 但我也不是iOS專長,只是當初這樣做能 01/21 03:09
5F:→ sableangle: 解決我的需求 所以來拋磚引玉一下 01/21 03:09
6F:→ sableangle: 通常這樣做我還會搭配 [keyWindow makeKeyAndVisibl 01/21 03:09
7F:→ sableangle: e]; 來服用,至少自己的專案跑起來都頭好壯壯,可能是 01/21 03:09
8F:→ sableangle: 因為我製作的framework主要會使用的都是UI相對單純的 01/21 03:09
9F:→ sableangle: 遊戲引擎專案,Unity 3D Cocos2d 之類的(他們都只有 01/21 03:09
10F:→ sableangle: 一個OpenGL畫布,其他都是引擎內部處理) 01/21 03:09
11F:→ sableangle: 如果要嚴謹一點的話 其實好想在framework的接口開一個 01/21 03:10
12F:→ sableangle: 參數把當前的viewcontroller傳入就可以了? 01/21 03:10
13F:推 fbifxxkma: 感謝回答~~ 01/24 17:04