You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过按钮复制textLabel20标签文本?解决UIPasteboard未找到报错

解决「Cannot find 'UIPasteboard' in scope」错误

问题原因

UIPasteboard是UIKit框架提供的类,你的代码文件没有导入UIKit框架,导致编译器无法识别这个类。

解决方法

  1. 在代码文件的最顶部添加UIKit的导入语句:
    import UIKit
    
  2. 确认你的视图控制器类继承自UIViewController(只有基于UIKit的视图控制器才能正常使用这类API)

修正后的完整代码

import UIKit

class YourViewController: UIViewController {
    @IBOutlet weak var textLabel20: UILabel!
    
    @IBAction func copyTitleSelected(_ sender: Any) {
        UIPasteboard.general.string = textLabel20.text
    }
}

额外提醒:如果是SwiftUI与UIKit混合的项目,在使用UIPasteboard的文件中同样需要导入UIKit框架。

内容的提问来源于stack exchange,提问作者Harry Stanford

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 09:07:01