如何正确访问swift中其他控制器的一个控制器变量和方法?

时间:2016-01-13 08:36:44

标签: ios swift

我在HomePageViewController中创建了uiSegmentedControl。分段控制有两个项目。当我选择第一项时,我使用displayContentController方法将sensorItemViewController内容添加为HomePageViewController中的子视图。当点击第二项时,我想访问SensorTabItemViewController类的方法,它的名字是来自HomePageViewConroller的reloadMyTableView。我从sensorItemVC访问但我得到“在解开一个Optional值时意外发现nil”异常。如何从HomePageViewControler正确访问SensorItemTabViewController?谢谢大家回复

HomePageViewController.swift:

let segmentedControlItems = ["Table", "RefreshTableView"]
    var viewControllerArray: Array<UIViewController> = []
    var segmentedControl : UISegmentedControl!
    var sensorItemVC: SensorTabItemViewController!


        class HomePageViewController: UIViewController,UIScrollViewDelegate  {

            override func viewDidLoad() {
                    super.viewDidLoad()
             segmentedControl=UISegmentedControl(items: segmentedControlItems)

                    segmentedControl.selectedSegmentIndex=0
                    segmentedControl.tintColor=myKeys.darkBlueColor
                    segmentedControl.layer.cornerRadius = 0.0;
                    segmentedControl.layer.borderWidth = 1.5
                    segmentedControl.frame=CGRectMake(0, frameHeight/2, frameWidth, 35)
                    segmentedControl.addTarget(self, action: "changeSegmentedControlItem", forControlEvents: .ValueChanged)
                    self.view.addSubview(segmentedControl)

            let  sensorItemViewController = self.storyboard!.instantiateViewControllerWithIdentifier("sensorTabItemViewController")
                viewControllerArray.append(sensorItemViewController)

            }

            func changeSegmentedControlItem(){

        print(segmentedControl.selectedSegmentIndex)

        if(segmentedControl.selectedSegmentIndex==0){
                displayContentController(viewControllerArray[0])
        }

        else{
         sensorItemVC.reloadMyTableView("Temp value", light: "Light value", noise: "noise Value", motion: "motion Value")

        }
    }
   func displayContentController (content:UIViewController)     {

       self.addChildViewController(content)
        print(self.segmentedControl.frame.height)
        content.view.frame=CGRectMake(0, self.frameHeight/2+self.segmentedControl.frame.height, self.frameWidth,
        self.frameHeight-(segmentedControl.frame.height*2+self.frameHeight/2))
        self.view.addSubview(content.view)
        content.didMoveToParentViewController(self)


    }


            }

SensorTabItemViewController。迅速如下:

class SensorTabItemViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {
    @IBOutlet weak var tableView: UITableView!

    let sensorName=["Sıcaklık Sensörü","Işık Sensörü","Gürültü Sensörü","Hareket Sensörü"]
    var sensorDetails=["","","",""]
    var sensorImages: Array<UIImage> = []
    override func viewDidLoad() {
        super.viewDidLoad()
        print("sensorTab")
        let tempImg=UIImage(named: "temp_ic") as UIImage?
        let lightImg=UIImage(named: "light_ic") as UIImage?
        let noiseImg=UIImage(named: "noise_ic") as UIImage?
        let motionImg=UIImage(named: "motion_ic") as UIImage?

        sensorImages.append(tempImg!)
        sensorImages.append(lightImg!)
        sensorImages.append(noiseImg!)
        sensorImages.append(motionImg!)



        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }




    func reloadTableView(){
       sensorDetails=[]
       sensorDetails.append(temp)
       sensorDetails.append(light)
       sensorDetails.append(noise)
       sensorDetails.append(motion)
       tableView.reloadData()
    }

    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return sensorName.count
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("sensorCell", forIndexPath: indexPath) as UITableViewCell
        cell.textLabel?.text=sensorName[indexPath.row]
        cell.imageView?.image=sensorImages[indexPath.row]
        cell.detailTextLabel?.text=sensorDetails[indexPath.row]
        return cell
    }


}

1 个答案:

答案 0 :(得分:1)

您永远不会设置sensorItemVC的值。这就是nil的原因。我想那个

let  sensorItemViewController = self.storyboard!.instantiateViewControllerWithIdentifier("sensorTabItemViewController")

应替换为

sensorItemVC = self.storyboard!.instantiateViewControllerWithIdentifier("sensorTabItemViewController") as! SensorTabItemViewController