diff --git a/uni_modules/native-button/utssdk/app-ios/index.uts b/uni_modules/native-button/utssdk/app-ios/index.uts index 7ee6499fef447ecd42facebac1138ece0575e885..ee8e02501a0435cd445e226410263bb861cca3be 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 1c4dcb902d471345a6b3a91ffc68c8cf11c1a88f..90abdc1526d9ae737764917e19f92de80045fe8e 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) } }