在运行时

时间:2016-10-18 14:16:30

标签: core-data delegates uitextfield swift3 uiswitch

我想从PropertiesViewController.swift获取5个UITextFields的值,从ConditionsViewController获取8个开关(两个类都是TabController的一部分)。我想在用户点击saveMeter()后将用户输入的值插入到CoreData中。我的问题是当我尝试从另一个类中访问两个视图时,我有所有CRUD操作。当我点击Properties或ConditionsViewController上的保存按钮时,即使我只是输入一个值,文本和开关值也会返回nil。

该类名为CRUDController,这是必须重要的代码:

类CRUDController:UIViewController {

var条件:ConditionsViewController

var属性:PropertiesViewController

init(){

//我在下面理解我正在创建一个新实例而不是原始

//但是迅速强迫我开始他们,我不知道其他方式去做。

//我也试过像这样引用它们:

// var conditions:ConditionsViewController var properties:PropertiesViewController

//来自函数saveMeter,但文本中的值仍为空。

   conditions = ConditionsViewController()
    properties =  PropertiesViewController()

    super.init(nibName: nil, bundle:nil)

}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

func saveMeter(){

    //conditions.setConditions()
    //properties.setProperties()

        //creating empty meter
        let m1 = Meter(context: MOC)

//这是我在Coredata中设置Meter对象的值

        //conditions values
        m1.newMeterCond1 = conditions.newMeterCond1Val
        m1.newMeterCond2 = conditions.newMeterCond2Val
        m1.newMeterCond3 = conditions.newMeterCond3Val
        m1.oldMeterCond1 = conditions.oldMeterCond1Val
        m1.oldMeterCond2 = conditions.oldMeterCond2Val
        m1.oldMeterCond3 = conditions.oldMeterCond3Val
        m1.oldMeterCond4 = conditions.oldMeterCond4Val
        m1.oldMeterCond5 = conditions.oldMeterCond5Val
        m1.cnc = conditions.CNCVal 

       //properties values                
       m1.outKW =  properties.OutKWFromTextField
        m1.outKWH = properties.OutKWHFromTextField
       m1.outKVARH = properties.OutKVARHFromTextField
       m1.multiplier = properties.multiplierFromTextField
       m1.newMeterNumber = properties.newMeterNumberFromTextField


        APPD.saveContext()

}

}

以下是我获取文本字段和开关的类:

类ConditionsViewController:UIViewController {

//variables to set

var newMeterCond1Val = Bool()
var newMeterCond2Val = Bool()
var newMeterCond3Val = Bool()
var oldMeterCond1Val = Bool()
var oldMeterCond2Val = Bool()
var oldMeterCond3Val = Bool()
var oldMeterCond4Val = Bool()
var oldMeterCond5Val = Bool()
var CNCVal = Bool()

@IBOutlet weak var oldMeterNumber: UILabel!
@IBOutlet weak var newMeterNumber: UILabel!
var oldMeterFromCell = String()
var newMeterFromTabChild = String()

//old meter conditions
@IBOutlet weak var oldMeterCond1: UISwitch!
@IBOutlet weak var oldMeterCond2: UISwitch!
@IBOutlet weak var oldMeterCond3: UISwitch!
@IBOutlet weak var oldMeterCond4: UISwitch!
@IBOutlet weak var oldMeterCond5: UISwitch!

//new meter conditions
@IBOutlet weak var newMeterCond1: UISwitch!
@IBOutlet weak var newMeterCond2: UISwitch!
@IBOutlet weak var newMeterCond3: UISwitch!


@IBOutlet weak var CNC: UISwitch!

@IBAction func saveMeter(_ sender: AnyObject) {

   //this is where I am calling the function from CRUDController
    CRUD.saveMeter()
}

}

类PropertiesViewController:UIViewController,UITextFieldDelegate {

var oldMeterFromCell = String()

//variables to set
var OutKWHFromTextField = Float()
var OutKWFromTextField = Float()
var OutKVARHFromTextField = Float()
var newMeterNumberFromTextField = String()
var multiplierFromTextField = Float()

@IBOutlet weak var OldMeterLabel: UILabel!

//new meter properties
@IBOutlet weak var OutKWH: UITextField!
@IBOutlet weak var OutKW: UITextField!
@IBOutlet weak var OutKVARH: UITextField!
@IBOutlet weak var newMeterNumber: UITextField!
@IBOutlet weak var multiplier: UITextField!

}

我尝试使用init()和必需的init()从每个类初始化UITextField和Switch,我也尝试使用方便的init(),我也用自定义set函数初始化它们。我还在CRUDController类的两个类上创建了一个实例,但是当我点击保存按钮时,文本值仍然为零。我不能使用segue,因为我有2个观点,即使我可以不想。我需要从CRUDController类访问原始的PropertiesController和ConditionsController,我不知道怎么做,因为我对委托的概念和如何使事情工作非常困惑所以我需要CODE来解决问题,没有概念

1 个答案:

答案 0 :(得分:0)

我发现可以解决此问题的唯一方法是将所有开关,UITextfields,操作等从StoryBoard中的不同视图连接到一个可以访问所有元素的单个类。这样就不需要实例化一个Class来访问另一个Class。