简体   繁体   English

在Swift中访问不同类别的商店

[英]Accessing outlets from different class in Swift

I'm not even 100% sure this belongs here, but I need some assistance. 我什至不能100%确定这属于这里,但我需要一些帮助。

Basically, I have 3 sets of files: Storyboard Viewcontroller w/ ViewController Nib > Controller Nib > Controller 基本上,我有3组文件:带ViewController Nib的Storyboard Viewcontroller> Controller Nib> Controller

The storyboard tableviewcontroller loads one of the nibs as a table cell, the other is just a view that is added on to the top of the primary storyboard view. 情节提要tableviewcontroller加载一个笔尖作为表格单元,另一个则只是添加到主要情节提要视图顶部的视图。

To try to illustrate what I am doing, here is the code: 为了说明我在做什么,下面是代码:

let pickerView = UINib(nibName: "DateTimePickerView", bundle: nil).instantiateWithOwner(nil, options: nil)[0] as! DateTimePickerView

    let testFrame = CGRect(x: 0, y: 0, width: view.frame.size.width, height: 200)

    pickerView.frame = testFrame
    ReserveView.addSubview(pickerView)
    pickerView.translatesAutoresizingMaskIntoConstraints = false

    NSLayoutConstraint(item: pickerView, attribute: .Leading, relatedBy: .Equal, toItem: view, attribute: .LeadingMargin, multiplier: 1.0, constant: 0).active = true

    NSLayoutConstraint(item: pickerView, attribute: .Trailing, relatedBy: .Equal, toItem: view, attribute: .TrailingMargin, multiplier: 1.0, constant: 0).active = true

    NSLayoutConstraint(item: pickerView, attribute: .Bottom, relatedBy: .Equal, toItem: view, attribute: .BottomMargin, multiplier: 1.0, constant: 0.0).active = true

This adds the nib to the bottom of the view that is displayed. 这会将笔尖添加到显示的视图的底部。 What I am trying to accomplish is this: 我想要完成的是:

In the other nib that is loaded as the table cell: there are buttons, labels, etc that I want to interact with from the pickerView that is drawn on top of the view. 在作为表单元格加载的另一个笔尖中:有一些按钮,标签等我想从视图顶部绘制的pickerView中进行交互。

Hopefully this image can help clarify what I am trying to do and you can tell me why this is wrong.. because it doesn't feel like its the right way. 希望这张图片可以帮助您弄清我要做什么,您可以告诉我为什么这是错误的..因为它感觉不正确。

Ultimately, the "pickerView" will appear on button click, then hide when the user selects and confirms the date. 最终,“ pickerView”将在单击按钮时出现,然后在用户选择并确认日期时隐藏。

Here is the photo: 这是照片: 我试图描述的图画

Here is the actual project, so you can better see what I am trying to do: 这是实际的项目,因此您可以更好地了解我正在尝试做什么: 实际项目SS

From what I understand you have a storyboard that loads a nib with a tableView and cells in it as well as a nib with the picker view. 据我了解,您有一个情节提要,其中加载了一个带有tableView和其单元格的笔尖以及一个带有选取器视图的笔尖。

If your storyboard holds a reference to the tableview and the content it needs to change, you could use closures with the pickerview. 如果情节提要板具有对表视图及其需要更改的内容的引用,则可以在pickerview中使用闭包

In your pickerview add the following: 在您的pickerview中添加以下内容:

var didChange: ((value: String)->())?

In your storyboard class add the following code: 在您的情节提要类中添加以下代码:

func pickerDidChange(value: String) {
    debugPrint("The picker changed")
}

Where you initialise your picker view, add the following just above the addSubview : 在初始化选择器视图的位置,将以下内容添加到addSubview上方:

pickerView.didChange = pickerDidChange

Now, in your pickerDidChange func you'll take the data you needed and apply it to the cells in the tableView, which you held a reference to. 现在,在您的pickerDidChange函数中,您将获取所需的数据并将其应用到您持有引用的tableView的单元格中。

In the pickerview you'll add the following in the onChange method: 在pickerview中,您将在onChange方法中添加以下内容:

func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    if let didChange = didChange {
        didChange("something")
    }
}

If you would be using a UIDatePicker instead, then return a NSDate instead... which I guess would be more appropriate. 如果您使用UIDatePicker代替,则返回NSDate ……我想这会更合适。

I hope I understood your question properly and this solves your issue. 希望我能正确理解您的问题,从而解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM