如何在另一个类中访问 IBOutlet?

2024-04-11

我在这个问题中遇到了同样的错误:我如何访问另一个类中的 IBOutlet?迅速 https://stackoverflow.com/questions/35358225/how-can-i-access-iboutlet-in-another-class-in-swift但是当我在 Xcode 中写入时(对于带有 Swift 3 的 iOS 8),我遇到了错误。

我的代码是这样的。我要编辑amauntOut (is an UILabel)在课堂上Convert_Table_Third_Cell只需一键操作:

@IBAction func actionTextb(_ sender: Any) {
    print("you writted \(String(describing: amauntEnter.text!))----")

    //Convert_Table_Third_Cell.amauntOut.text = amauntEnter.text ----> is a tried
    //var dash : abcViewController = storyBoard.instantiateViewControllerWithIdentifier("abc") as! abcViewController ----> is a tried
    //var a = dash.getXYZ() ----> is a tried

    var update: Convert_Table_Third_Cell = UIStoryboard.instantiateViewController(UIStoryboard) as! Convert_Table_Third_Cell
    update.amauntOut.text = "hola"
}

我收到此错误:

实例成员“instantiateViewController”不能用于类型“UIStoryboard”;您的意思是使用这种类型的值吗?

有人能帮我吗?

这是第一堂课

import UIKit

class Convert_Table_Second_Cell: UITableViewCell {

    @IBOutlet var amauntEnter: UITextField!

    var theNumber = getTheNumber()



    @IBAction func actionTextb(_ sender: Any) {
        print("you writted \(String(describing: amauntEnter.text!))----")

        let storyboard = UIStoryboard.init(name: "convert ID", bundle: nil)
        let update = storyboard.instantiateViewController(withIdentifier:  "convert ID") as!  Convert_Table_Third_Cell

        update.amauntOut.text = "hola"


        let aa = "hola hillel----------------------"
        print(aa)
        print(theNumber.usedParameters(ArrayOfNumbers: unitInOutlet, TipOfData: 3))

    }

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        print("this is the valeu \(theNumber.hola)")
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}

这是第二个我要编辑的标签

导入 UIKit

类 Convert_Table_Third_Cell: UITableViewCell {

@IBOutlet var amauntOut: UILabel!

@IBOutlet var UnityMeasurment: UILabel!


override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
}

override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    // Configure the view for the selected state
}

}


你的做法是不正确的。当视图控制器显示在屏幕上时,它就会被启动。一次只能显示一个视图控制器对象。在您的代码中,您正在启动一个全新的视图控制器并将文本设置为出口。所以那是行不通的。相反,您需要将文本设置为视图控制器现有实例上的文本字段。

为此,在要接收文本字段内容更新的视图控制器中,在通知中心注册以接收内容更新函数调用。

NotificationCenter.default.addObserver(self, selector: #selector(listnerFunction(_:)), name: NSNotification.Name(rawValue: "notificationName"), object: nil)

func listnerFunction(_ notification: NSNotification) {
    if let data = notification.userInfo?["data"] as? String {
        self.textField.text = data
    }
}

然后在另一个视图控制器中,如果您想将文本发送到上面的视图控制器并更新文本,只需将数据发布到通知中心即可

let data:[String: String] = ["data": "YourData"]
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "notificationName"), object: nil, userInfo: data) 
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在另一个类中访问 IBOutlet? 的相关文章

  • 使用 nib 作为带有 nib 类的表节标题

    我想创建一个加载 nib 文件并将其设置为标题 UIView 的节标题 这个 nib 文件还将有一个关联的类 其中插座和操作连接到 因此我想像平常一样使用 nib 加载该类 我在网上搜索并找到了几个类似的答案 但我找不到任何适合我的答案 经
  • 来自索引范围 Swift 的新数组

    我怎样才能做这样的事情 从数组中取出前 n 个元素 newNumbers numbers 0 n 目前出现以下错误 error could not find an overload for subscript that accepts th
  • iOS 13 检查 CLLocationManager 的临时授权状态

    根据 WWDC 视频 https developer apple com videos play wwdc2019 705 https developer apple com videos play wwdc2019 705 当你要求 Al
  • iOS uiwebview 在 WebThread 中崩溃

    我正在寻求一些建议或帮助诊断我所看到的这次崩溃 目前 我认为这可能是一个 webkit 错误 但一切皆有可能 因此请提供您可能有的任何见解 Incident Identifier AEB8EE37 E5D4 4975 97F4 2B2038
  • 如何在应用程序项目中使用 Cocoa Touch 框架

    我熟悉构建单个 iOS 应用程序 但我坚持使用 Cocoa Touch 框架向多个应用程序共享通用代码 问题 框架的头文件不可见 无法链接到消费应用程序项目 我做了什么 1 我创建了一个名为 libTestFramework 的项目 Coc
  • 如何在 Swift 中使用 CoreBluetooth 更新 BLE 设备的电池电量?

    func peripheral peripheral CBPeripheral didDiscoverCharacteristicsFor service CBService error Error for c in service cha
  • IDFA 使用不当,您的应用不遵守 ios 中的限制广告跟踪设置

    I have checked the iTC settings I have uploaded the same app 2 days ago and it works fine but when today I uploaded the
  • SceneKit unproject Z 文档解释?

    我正在经历一些 SceneKit 概念 而我试图在脑海中巩固的一个概念是 unprojectPoint 我知道该函数将获取 2D 中的一个点并返回 3D 中的一个点 因此具有正确的 Z 值 当我阅读文档时 我读到了以下内容 method u
  • Swift:检查 UISearchBar.text 是否包含 url

    如何检查 UISearchBar text 是否包含 URL 我想做这样的事情 if searchBar text NSTextCheckingType Link 但我收到错误 String is not convertible to NS
  • 在 ios 版 Ionic 中接收 URL

    我正在使用离子框架 我正在尝试设置一种从另一个应用程序接收网址的方法 就像 您在浏览器中 单击共享 然后将链接发送到另一个应用程序 我的应用程序 我找到了这个cordova https stackoverflow com questions
  • 在 UITableViewController 中重新排序行后 UI 更新不正确

    因此 我对表中的行重新排序 用户界面最终结果不正确 场景如下 表内容原文 a b c d e 如果我移动第 0 行 当前a 到第 4 行 当前e 我看到的最终结果是 c d e a a 一些背景 该表正在读取 Realm 对象的列表 我确认
  • NSURLCache 不缓存

    我正在使用 Xcode 6 1 6A1030 iOS7 和 iOS8 模拟器 NSURLCache 似乎没有缓存任何东西 我使用 Cache Control 标头 我的服务器返回带有 max age 6000 的 Cache Control
  • 如何将CIFilter应用到UIView上?

    根据Apple docs 过滤属性CALayer不支持iOS 当我使用正在申请的应用程序之一时CIFilter to UIView即 Splice Funimate 和 Artisto 的视频编辑器 Videoshow FX 这意味着我们可
  • Swift:协议、结构、类

    我开始学习 Swift 语言 但在理解协议 结构和类方面遇到了困难 我来自 Android 方面的编程 所以我相信 Swift 协议基本上是 Java 接口 其中每一个的正确用例是什么 这些类比并不 完全 正确 但这就是我所理解的要点 是的
  • 命令 /Applications/Xcode-beta.app/Contents/Developer/usr/bin/atool 失败,退出代码 255

    它发生在新的 Xcode7 beta 中 CompileAssetCatalog Users admin Library Developer Xcode DerivedData InstaAd ddgdnbxlpxipirebpndfmgr
  • 循环多个 UIAlertController

    在某些情况下 我的应用程序需要显示多个警报消息 错误消息在启动时收集 并且需要一次向用户显示一条 当第一个被确认后 应该呈现下一个 问题在于 显然 它们都试图同时执行 有没有一种聪明的方法可以同步执行此操作 这是一些简单描述我想要做的事情的
  • 带有 allowedEditing 的 UIImagePickerController 不允许平移裁剪

    我在这里看到这个问题 UIImagePicker 允许编辑卡在中心 https stackoverflow com questions 12630155 uiimagepicker allowsediting stuck in center
  • 如何在Sprite Kit中实现鼠标关节?

    我已经在 iOS 上用 Cocos2d Box2d 编写了拖放功能的工作实现 我需要将它移植到 Sprite Kit 逻辑非常基本 当用户触摸屏幕时 找到手指下的精灵 在找到的精灵和场景的物理体之间创建鼠标关节 将关节的目标设置为触摸位置
  • 下载进度条在 iOS 企业发行版中没有改变进度

    我正在通过企业分发开发和分发 iPad 应用程序 它们下载并执行良好 因此一切正常 Web 链接 ipa 文件 plist 文件 配置 问题 是 当用户单击链接进行下载时 iPad 中显示下载进度的进度条显示 正在等待 但却是空的并且永远不
  • 如何将自定义 C 代码放入 SwiftPM 包中?

    我正在尝试将 C 代码打包到 Swift 模块中 我们称之为CModule 一旦我将其放入项目的基本文件夹中 Swift模块 并配置了搜索路径 我可以在 Swift 文件中自动完成工作 并检测错误 警告 问题是 导入时它无法识别该模块 并且

随机推荐