2016-01-13 48 views
0

我在HomePageViewController中創建了uiSegmentedControl。分段控制有兩個項目。當我選擇第一項時,我使用displayContentController方法在HomePageViewController中將sensorItemViewController內容添加爲子視圖。當點擊第二項時,我想訪問SensorTabItemViewController類的方法,它的名稱是來自HomePageViewConroller的reloadMyTableView。我從sensorItemVC訪問,但我得到「意外地發現零,而解包可選值」異常。如何正確訪問HomePageViewControler的SensorItemTabViewController?謝謝大家響應如何從swift中正確訪問其他控制器中的一個控制器變量和方法?

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。 swift如下:

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

更換如何這些控制器顯示?我看到你聲明'sensorItemVC' var,但你永遠不會分配它。 – kelin

+0

我該如何分配? – yonder

+0

使用assign操作符,在大多數語言中調用'=',包括swift。 – SmokeDispenser

回答

1

您從未設置sensorItemVC的值。這就是爲什麼它是nil。我想這

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

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

我得到這個警報:不能分配值類型「UIViewController」類型SensorTabItemViewController – yonder

+0

更新我的答案(忘記了類型轉換) – Silmaril

+0

謝謝你節省我的時間。我是swift和ios編程的新手。 – yonder

相關問題