From eb0e98b04cd490cd9c1eb8aff86aee037e16e106 Mon Sep 17 00:00:00 2001 From: XHY Date: Fri, 13 Sep 2024 16:28:36 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4=20native-view=20=E7=A4=BA?= =?UTF-8?q?=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../native-button/utssdk/app-ios/index.uts | 48 ++++++++++--------- .../utssdk/app-ios/index.uts | 2 +- 2 files changed, 26 insertions(+), 24 deletions(-) diff --git a/uni_modules/native-button/utssdk/app-ios/index.uts b/uni_modules/native-button/utssdk/app-ios/index.uts index 7ee6499f..ee8e0250 100644 --- a/uni_modules/native-button/utssdk/app-ios/index.uts +++ b/uni_modules/native-button/utssdk/app-ios/index.uts @@ -1,46 +1,48 @@ -import { UIButton, UIControl,ButtonType } from "UIKit" +import { UIButton, UIControl } from "UIKit" export class NativeButton { element : UniNativeViewElement; - button : UIButton; + button : UIButton | null; - constructor(element : UniNativeViewElement) { + constructor(element : UniNativeViewElement) { + // 接收组件传递过来的UniNativeViewElement this.element = element; - this.button = new UIButton(type=UIButton.ButtonType.system) - super.init() - - // 在 swift target-action 对应的方法需要以OC的方式来调用,那么OC语言中用Selector来表示一个方法的名称(又称方法选择器),创建一个Selector可以使用 Selector("functionName") 的方式。 - const method = Selector("buttonClickAction") - // button 添加点击回调 - button.addTarget(this, action = method, for = UIControl.Event.touchUpInside) - - this.bind(); + super.init() + this.bindView(); } // element 绑定原生view - bind() { - this.element.bindIOSView(this.button); + bindView() { + // 初始化原生 UIButton + this.button = new UIButton(type=UIButton.ButtonType.system) + // 构建方法选择器 + const method = Selector("buttonClickAction") + // button 绑定点击回调方法 + button?.addTarget(this, action = method, for = UIControl.Event.touchUpInside) + // UniNativeViewElement 绑定原生 view + this.element.bindIOSView(this.button!); } - - // 更新button文字 - updateText(text : string) { - this.button.setTitle(text, for = UIControl.State.normal) + + updateText(text : string) { + // 更新 button 显示文字 + this.button?.setTitle(text, for = UIControl.State.normal) } /** * 按钮点击回调方法 * 在 swift 中,所有target-action (例如按钮的点击事件,NotificationCenter 的通知事件等)对应的 action 函数前面都要使用 @objc 进行标记。 */ - @objc buttonClickAction() { - // 发送事件 + @objc buttonClickAction() { + //构建自定义 UniNativeViewEvent 对象 let event = new UniNativeViewEvent("customClick") + //触发自定义事件 this.element.dispatchEvent(event) } - - destroy() { - UTSiOS.destroyInstance(self) + destroy() { + // 释放 UTS 实例对象,避免内存泄露 + UTSiOS.destroyInstance(this) } } diff --git a/uni_modules/native-time-picker/utssdk/app-ios/index.uts b/uni_modules/native-time-picker/utssdk/app-ios/index.uts index 1c4dcb90..90abdc15 100644 --- a/uni_modules/native-time-picker/utssdk/app-ios/index.uts +++ b/uni_modules/native-time-picker/utssdk/app-ios/index.uts @@ -80,6 +80,6 @@ export class NativeTimePicker { } destroy() { - UTSiOS.destroyInstance(self) + UTSiOS.destroyInstance(this) } } -- GitLab