actionSheet报错的问题及解决方法

本文详细介绍了在开发应用时遇到的问题:在iPhone模拟器上成功调用系统相册与相机功能,但在iPad模拟器上点击事件失效并报错。通过将代码中的clickedButtonAtIndex方法修改为didDismissWithButtonIndex,解决了冲突问题。同时,分享了一个在imagePickerController中结束调用导致程序崩溃的bug,并提出疑问寻求解答。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近写一个调用系统相册和系统相机的功能,其实很简单,然后自己实现一遍的时候出现了一个问题,如下:


在需要调用系统相册与相机的时候弹出一个actionSheet,
然后在actionSheetDelegate里面找了个clickedButtonAtIndex方法就写
了点击事件,运行iPhone模拟器,一切都显得那么OK,然而
运行ipad模拟器的时候,点击没有效果,console里面报了一个错

Warning: Attempt to present <UIImagePickerController: 
0x7a085000>  on <ViewController: 0x79863ba0> which is
 already presenting <UIAlertController: 0x7873b4b0>

很容易理解,但是当时真没有细看,翻译一下

警告:尝试在已经呈现了一个UIAlertController
的viewController上呈现UIImagePickerController.

这里的UIAlertController就是指的已经弹出的actionSheet
控制器。

所以真相大白了,解决方案就是把clickedButtonAtIndex方法改为didDismissWithButtonIndex,这样present imagePickerController的时候actionSheet就已经不存在了,上面的冲突也就不存在了。
至于为何iPhone上不存在问题而ipad上有此问题,希望大神能跟帖留言,给出答案,也能实现我抛砖引玉的美好愿望。

*   附:最近遇到了一个让我困扰很久的bug,也在这里贴一下:
目前这个项目是ipad项目,因为需要写死横屏,然而
在imagePickerController这个只支持竖屏的玩意儿调用
结束,给它dismiss掉的时候,程序立马崩掉,始终不得解。
以上代码替换原代码编译后还是出现下列报错信息,1、模拟器中有复杂计算逻辑要求终止;2、 weui-miniprogram --save在历史记录中已经删除了,原来的组件中有该文件的呀,这里为什么还会报错呢?3、workers/index.js文件存在的呀,该路径下的原代码是你历史记录中给我的代码;4、[Deprecation] SharedArrayBuffer will require cross-origin isolation as of M92, around July 2021. See https://developer.chrome.com/blog/enabling-shared-array-buffer/ for more details. [system] WeChatLib: 3.8.11 (2025.7.17 17:36:09) [system] No. of subpackages: 4 [system] LazyCodeLoading: true Lazy code loading is enabled. Only injecting required components. [wxobs] auto recording mode is not enabled in devtools. [体验分析] 为避免影响调试,开发者工具在非自定义启动模式下不会启动采集。详见 https://dev.weixin.qq.com/docs/analysis/sdk/debugging.html app.js:37 云环境初始化成功 [Perf] App.initCloud took 298ms onLaunch @ app.js:7 (anonymous) @ app.js:1 async function (async) (anonymous) @ appservice.app.js:5 [Perf] App.onLaunch took 314ms (anonymous) @ app.js:1 async function (async) (anonymous) @ appservice.app.js:5 app.js:45 Worker初始化开始... wx.getSystemInfoSync is deprecated.Please use wx.getSystemSetting/wx.getAppAuthorizeSetting/wx.getDeviceInfo/wx.getWindowInfo/wx.getAppBaseInfo instead. initWorker @ app.js:48 (anonymous) @ app.js:16 listOnTimeout @ node:internal/timers:557 processTimers @ node:internal/timers:500 [skyline] skyline 版本号: 1.4.0, 版本记录: 8f190450e6301587ce41e08afcaa983db4dc712e app.js:61 当前环境不支持Worker (anonymous) @ app.js:61 listOnTimeout @ node:internal/timers:557 processTimers @ node:internal/timers:500 app.js:163 开始验证WeUI路径... app.js:179 路径不存在: miniprogram_npm/weui-miniprogram/package.json (anonymous) @ app.js:179 verifyWeuiPath @ app.js:174 (anonymous) @ app.js:19 listOnTimeout @ node:internal/timers:557 processTimers @ node:internal/timers:500 app.js:179 路径不存在: miniprogram_npm/weui-miniprogram/actionsheet/actionsheet.wxml (anonymous) @ app.js:179 verifyWeuiPath @ app.js:174 (anonymous) @ app.js:19 listOnTimeout @ node:internal/timers:557 processTimers @ node:internal/timers:500 [Perf] App.verifyWeuiPath took 60ms (anonymous) @ app.js:19 listOnTimeout @ node:internal/timers:557 processTimers @ node:internal/timers:500 [Deprecation] SharedArrayBuffer will require cross-origin isolation as of M92, around July 2021. See https://developer.chrome.com/blog/enabling-shared-array-buffer/ for more details.[worker] reportRealtimeAction:fail not support。请你根据上以上预警信息和历史记录中给我的代码进行修复以上所有问题,并给我修复完成之后的完整的代码和代码路径。
最新发布
07-26
根据历史记录中的代码,我将你给我的代码进行替换,保存编译后,模拟器还出现下列错误信息,请你告诉我具体修复步骤和完整代码;报错信息如下:1、模拟器长时间没有响应,请确认你的业务逻辑中是否有复杂运算,或者死循环。2、invalid app.json ["setting"] [Deprecation] SharedArrayBuffer will require cross-origin isolation as of M92, around July 2021. See https://developer.chrome.com/blog/enabling-shared-array-buffer/ for more details. [system] WeChatLib: 3.8.11 (2025.7.17 17:36:09) [system] No. of subpackages: 4 [system] LazyCodeLoading: true Lazy code loading is enabled. Only injecting required components. [wxobs] auto recording mode is not enabled in devtools. [体验分析] 为避免影响调试,开发者工具在非自定义启动模式下不会启动采集。详见 https://dev.weixin.qq.com/docs/analysis/sdk/debugging.html invalid app.json ["setting"] app.js:21 应用启动失败: TypeError: this.initCloud is not a function at Rt.onLaunch (app.js:7) at Rt.<anonymous> (VM674 WASubContext.js:1) at new Rt (VM674 WASubContext.js:1) at t.<anonymous> (VM674 WASubContext.js:1) at VM674 WASubContext.js:1 at app.js:1 at VM674 WASubContext.js:1 at _.runWith (VM674 WASubContext.js:1) at q (VM674 WASubContext.js:1) at appservice.js:7(env: Windows,mp,1.06.2504010; lib: 3.8.11) onLaunch @ app.js:21 (anonymous) @ app.js:1 async function (async) (anonymous) @ appservice.app.js:5 interfacebuilder disabled invoke showToast {id: 0, duration: 2000, title: "应用启动失败", icon: "error", mask: false, …} [skyline] skyline 版本号: 1.4.0, 版本记录: 8f190450e6301587ce41e08afcaa983db4dc712e。3、我提供app.json和app.js原代码给你,请你帮我分析并进行修复,然后给我修复之后的完整的代码;app.js原代码:App({ onLaunch(options) { try { this.cloudEnvId = 'cloud1-8gtlpqtpc174de7f'; // 初始化云环境 this.initCloud(this.cloudEnvId); // 安全区域适配(使用最新API) this.initSafeArea(); // 授权获取用户信息(使用最新API) this.checkAuthStatus(); // 添加Worker安全初始化(兼容性检查) this.initWorker(); // 5秒后验证WeUI路径(避免干扰初始化) setTimeout(() => this.verifyWeuiPath(), 5000); } catch (e) { console.error('应用启动失败:', e); wx.showToast({ title: '应用启动失败', icon: 'error', duration: 2000 }); } }, // Worker安全初始化(完全兼容处理) initWorker() { console.log('-----------------------------'); console.log('Worker初始化开始...'); // 基础库版本检查 const SDKVersion = wx.getSystemInfoSync().SDKVersion; const minVersion = '2.20.1'; if (this.compareVersion(SDKVersion, minVersion) < 0) { console.warn(`基础库版本过低(${SDKVersion} < ${minVersion}),不支持Worker`); this.globalData.workerAvailable = false; return; } if (typeof Worker === 'undefined' || !wx.createWorker) { console.warn('当前环境不支持Worker'); this.globalData.workerAvailable = false; return; } try { this.worker = wx.createWorker('workers/index'); console.log('Worker创建成功'); this.worker.postMessage({ type: 'test', message: 'Worker通信测试' }); this.worker.onMessage((res) => { if (res.type === 'test') { console.log('收到Worker测试回复:', res); this.globalData.workerAvailable = true; } }); } catch (e) { console.error('Worker创建失败:', e); this.globalData.workerAvailable = false; } console.log('-----------------------------'); }, // 版本比较工具 compareVersion(v1, v2) { v1 = v1.split('.'); v2 = v2.split('.'); for (let i = 0; i < Math.max(v1.length, v2.length); i++) { const num1 = parseInt(v1[i] || 0, 10); const num2 = parseInt(v2[i] || 0, 10); if (num1 !== num2) return num1 - num2; } return 0; }, // 安全区域适配(使用最新API) initSafeArea() { let safeAreaInsets = { bottom: 0, top: 0, left: 0, right: 0 }; try { // 使用推荐的wx.getWindowInfo方法 if (typeof wx.getWindowInfo === 'function') { const windowInfo = wx.getWindowInfo(); safeAreaInsets = windowInfo.safeArea; } // 兼容老版本API else if (typeof wx.getSystemInfoSync === 'function') { const systemInfo = wx.getSystemInfoSync(); safeAreaInsets = systemInfo.safeArea || systemInfo.safeAreaInsets || safeAreaInsets; } // 设置全局CSS变量 this.setGlobalCssVariables(safeAreaInsets); } catch (e) { console.warn('安全区域获取失败', e); } this.globalData.safeAreaInsets = safeAreaInsets; console.log('安全区域设置完成:', safeAreaInsets); }, // 设置全局CSS变量 setGlobalCssVariables(safeAreaInsets) { try { wx.setCssVariables({ '--safe-area-inset-top': `${safeAreaInsets.top || 0}px`, '--safe-area-inset-bottom': `${safeAreaInsets.bottom || 0}px`, '--safe-area-inset-left': `${safeAreaInsets.left || 0}px`, '--safe-area-inset-right': `${safeAreaInsets.right || 0}px` }); } catch (e) { console.warn('CSS变量设置失败', e); } }, // 授权获取用户信息(兼容新老API) checkAuthStatus() { try { let authSetting = {}; // 最新推荐API if (typeof wx.getAppAuthorizeSetting === 'function') { authSetting = wx.getAppAuthorizeSetting() || {}; } // 兼容老版本API else if (typeof wx.getSetting === 'function') { const res = wx.getSetting({ sync: true }); authSetting = res.authSetting || {}; } // 最后的兼容方案 else if (typeof wx.getSettingSync === 'function') { const res = wx.getSettingSync(); authSetting = res.authSetting || {}; } if (!authSetting['scope.userInfo']) { wx.reLaunch({ url: '/pages/login/login' }); } } catch (e) { console.warn('授权状态检查失败', e); // 即使在授权失败情况下也不阻止应用启动 } }, // WeUI路径验证 verifyWeuiPath() { console.log('-----------------------------'); console.log('开始验证WeUI路径...'); // 使用安全的文件系统操作 try { const fs = wx.getFileSystemManager(); const pathsToCheck = [ 'miniprogram_npm/weui-miniprogram/package.json', 'miniprogram_npm/weui-miniprogram/actionsheet/actionsheet.wxml' ]; let existsCount = 0; pathsToCheck.forEach(path => { try { fs.accessSync(path); console.log(`✅ 路径存在: ${path}`); existsCount++; } catch (accessErr) { console.warn(`❌ 路径不存在: ${path}`); } }); if (existsCount === 0) { console.error('关键WeUI文件缺失!'); this.showWeuiInstallGuide(); } else if (existsCount > 1) { console.log('WeUI验证通过'); } } catch (e) { console.error('文件系统操作出错:', e); } console.log('-----------------------------'); }, // 显示WeUI安装指南(替代自动安装) showWeuiInstallGuide() { const guideMessage = ` WeUI组件未正确安装,请按以下步骤操作: 1. 打开项目目录(E:/LIANGYE99999) 2. 运行命令:npm install weui-miniprogram --save 3. 在微信开发者工具中:工具 -> 构建 npm 4. 清除缓存并重新编译 `; wx.showModal({ title: 'WeUI组件缺失', content: guideMessage, showCancel: false, confirmText: '知道了' }); console.warn(guideMessage); }, // 全局数据 globalData: { userInfo: null, unreadCount: 0, safeAreaInsets: { bottom: 0, top: 0, left: 0, right: 0 }, cloudEnvId: '', workerAvailable: false, projectPath: 'E:/LIANGYE99999' } }); app.json原代码:{ "pages": [ "pages/welcome/welcome", "pages/login/login", "pages/home/home", "pages/autoService/autoService", "pages/products/products", "pages/financeCircle/financeCircle", "pages/profile/profile", "pages/doc/upload/upload", "pages/contract/list/list", "pages/setting/setting", "pages/setting/constant/constant" ], "subPackages": [ { "root": "subpackages", "pages": [ "financeCircle/pages/publish/index", "auth/pages/realname/realname", "oa/pages/home/home", "oa/pages/todo/todo", "oa/pages/meetings/meetings", "oa/pages/approvals/approvals", "financeCircle/pages/messages/system", "financeCircle/pages/messages/finance", "financeCircle/pages/messages/interaction", "financeCircle/pages/index/index", "financeCircle/pages/publish/publish", "financeCircle/pages/detail/detail", "carWarranty/infoInput/carInfo", "carWarranty/eSign/eSign", "carWarranty/contract/templateLibrary/templateLibrary" ] }, { "root": "pages/charts", "pages": ["charts"] }, { "root": "subpages/finance", "pages": ["circle", "detail"] }, { "root": "subpages/products", "pages": ["category", "list"] } ], "window": { "backgroundColor": "#F7F9FC", "backgroundTextStyle": "light", "navigationStyle": "custom", "navigationBarBackgroundColor": "#1A3A84", "navigationBarTextStyle": "white" }, "tabBar": { "color": "#999999", "selectedColor": "#1A3A84", "backgroundColor": "#FFFFFF", "borderStyle": "white", "position": "bottom", "list": [ { "pagePath": "pages/home/home", "text": "首页", "iconPath": "images/icon/home.png", "selectedIconPath": "images/icon/home_active.png" }, { "pagePath": "pages/autoService/autoService", "text": "汽车企服", "iconPath": "images/icon/car.png", "selectedIconPath": "images/icon/car_active.png" }, { "pagePath": "pages/products/products", "text": "产品大全", "iconPath": "images/icon/product.png", "selectedIconPath": "images/icon/product_active.png" }, { "pagePath": "pages/financeCircle/financeCircle", "text": "易融圈", "iconPath": "images/icon/finance.png", "selectedIconPath": "images/icon/finance_active.png" }, { "pagePath": "pages/profile/profile", "text": "我的", "iconPath": "images/icon/user.png", "selectedIconPath": "images/icon/user_active.png" } ] }, "permission": { "scope.userLocation": { "desc": "用于展示本地服务机构" } }, "requiredPrivateInfos": ["getLocation"], "style": "v2", "sitemapLocation": "sitemap.json", "preloadRule": { "pages/home/home": { "network": "wifi", "packages": ["subpackages"] } }, "renderer": "skyline", "componentFramework": "glass-easel", "lazyCodeLoading": "requiredComponents", "rendererOptions": { "skyline": { "defaultDisplayBlock": true, "disableABTest": true, "defaultContentBox": true } }, "workers": "workers", "setting": { "minSdkVersion": "2.29.2", "urlCheck": true, "es6": true, "postcss": true, "babelSetting": { "ignore": [], "disablePlugins": [], "outputPath": "" }, "useWorker": true }, "useExtendedLib": { "weui": true } }
07-26
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值