從去年11月份,開始搗鼓 Weex 相關的一些東西,直到現在也踩了不少的坑,前兩天在公司內部搞了個小分享,現在把分享的部分東西貼出來供大家參考。
前提是:你 iOS 工程中已經接入了 WeexSDK,如果還沒接入可以查看我的另一篇博客,地址:http://www.cnblogs.com/shisishao/p/6439850.html,有集成 WeexSDK 到工程中的方法;
一、自定義事件Module流程:
1、自定義事件Module(客戶端准備):
1.1、先自定義 module 類,然后必須遵循 WXModuleProtocol 協議
1.2、必須添加宏 WX_EXPORT_METHOD, 它可以被 weex 識別,它的參數是 JavaScript 調用 module 指定方法的參數
1.3、添加 @synthesized weexInstance,每個moudle對象被綁定到一個指定的實例上
1.4、Weex 的參數可以是 String 或者Map
1.5、Module 支持返回值給 JavaScript中的回調,回調的類型是 WXModuleCallback, 回調的參數可以是 String 或者 Map
2、注冊 module(Weex 和 Native 建立連接)
通過調用 WXSDKEngine 中的 registerModule:withClass 方法來注冊自己的 module,例如:
[WXSDKEngine registerModule:@"event" withClass:NSClassFromString(@"WXEventModule")];
注冊完之后,也就是已經和Weex建立了連接。
二、自定義原生控件流程:
1、自定義控件(客戶端准備):
1.1、先創建一個繼承自WXComponent的類重寫以下方法:
- (instancetype)initWithRef:(NSString *)ref type:(NSString *)type styles:(NSDictionary *)styles attributes:(NSDictionary *)attributes events:(NSArray *)events weexInstance:(WXSDKInstance *)weexInstance;
// 加載控件
- (UIView *)loadView;
// 更新樣式
- (void)updateStyles:(NSDictionary *)styles;
// 回傳數據:
- (void)fireEvent:(NSString *)eventName params:(NSDictionary *)params;
2、注冊控件(Weex 和 Native 建立連接):
2.1、注冊一個 component ,調用 WXSDKEngine 中的 registerComponent:withClass: 方法。
2.2、Native注冊:
[WXSDKEngine registerComponent:@"show_image_view" withClass:NSClassFromString(@"KBHShowImageViewWXComponent")];
2.3、Weex實現:
<show_image_view class="my_cus_view"
@refresh="onrefresh"
:style="{ height: after_height + 'px'}"
value="show"
view_id="2"
:imglist="afterMedia"></show_image_view>
// 實現該方法
onrefresh: function (result) {}
如果轉載,請注明出去:http://www.cnblogs.com/shisishao/p/Weex.html