ios - Xcode( swift ): Changing UIButton attributes in a different UIViewController class than that in which they were referenced

标签 ios xcode swift uibutton

在我提出实际问题之前的快速说明:我对 XCode 和 Swift 还很陌生(刚接触几天)。我在 matlab、mathematica 和 IGOR 等科学平台上有编程经验。

我的问题: 在我的主视图 Controller 中,我有一个用于 UIButton 的 IBOutlet,我已经在主 Storyboard中实现了它。

class ViewController: UIViewController {

    ....    

    @IBOutlet weak var chip_a1: UIButton!

    ....
}

同样在我的主视图 Controller 中,在 IBAction 方法中,我能够按如下方式访问隐藏属性:

@IBAction func chip_a1_touchupinside(sender: UIButton) {
    self.chip_a1.hidden = true
}

但是当我尝试从不同的 View Controller 类中访问此属性时,如下所示:

class endgame: UIViewController {  
    ....  
    @IBAction func playagain_touchupinside(sender: UIButton) {
        var handle = ViewController()
        // make the states of all the chips visible
        handle.chip_a1.hidden = false  // this is the line that gives me an ERROR
        dismissViewControllerAnimated(true, completion: nil)
    }
    ....
}

错误是这样的:

Thread 1: EXC_BAD_INSTRUCTION (code=EXC_1386_INVOP, subcode=0x0)

如果您能帮助理解/解决此问题,我们将不胜感激。如果需要,我很乐意提供更多详细信息。

最好的, 亚历克斯。

最佳答案

你用 xib 文件创建 UIViewcontroller

你必须改变

var handle = ViewController()

var handle = ViewController(nibName: "ViewControllerNibfileName", bundle: nil)

因为 Button 是 IBOutlet 并且需要用 viewcontroller 加载的 nibfile

关于ios - Xcode( swift ): Changing UIButton attributes in a different UIViewController class than that in which they were referenced,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27635162/

相关文章:

ios - 在 Swift 中调整通用 PDF 图像资源的大小会产生模糊的图像

swift - 想知道这是否是执行此操作的最佳方式。 tableview 切换单元格大小

ios - 即使从 Facebook 应用程序设置中删除应用程序后,用户仍可以访问应用程序

ios - 如何使插入 UINavigationViewController 的 View 正确调整大小?

ios - XCTest 中的屏幕比例因子

ios - 无法使用类型为 '*' 的参数列表调用 '' - Swift 泛型问题

ios - pod 安装 [!] 错误 : Parsing unable to continue due to parsing error:

ios - 如何使用 MPVolumeView 来更改音频输出设备。喜欢 WhatsApp 和视频群聊

python - 套接字编程 readStream!.takeUnretainedValue() 需要无限时间

c++ - 使用 Opencv CvNormalBayesClassifier 训练皮肤像素