简体   繁体   English

如何正确快速地从另一个控制器访问一个控制器变量和方法?

[英]How to access one controller variable and methods from other controller in swift correctly?

I create uiSegmentedControl in HomePageViewController. 我在HomePageViewController中创建uiSegmentedControl。 There are two items in segmented control. 分段控件中有两个项目。 When I select first item , I add sensorItemViewController content as a subview in HomePageViewController with displayContentController method. 当我选择第一个项目时,我使用displayContentController方法将sensorItemViewController内容添加为HomePageViewController中的子视图。 And when clicked second item, I want to access methods of SensorTabItemViewController class which it's name is reloadMyTableView from HomePageViewConroller. 当单击第二项时,我想访问SensorTabItemViewController类的方法,该方法的名称是HomePageViewConroller中的reloadMyTableView。 I accessed from sensorItemVC but I get "unexpectedly found nil while unwrapping an Optional value" exception. 我从sensorItemVC访问,但是出现了“在包装可选值时意外发现nil”的异常。 How can access SensorItemTabViewController from HomePageViewControler correctly ? 如何从HomePageViewControler正确访问SensorItemTabViewController? Thank you all response 谢谢大家的回应

HomePageViewController.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. SensorTabItemViewController。 swift as below : 迅速如下:

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
    }


}

You never set the value of sensorItemVC . 您永远不会设置sensorItemVC的值。 That is why it is nil . 这就是为什么它nil I guess that 我猜是

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

should be replaced with 应该替换为

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

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

相关问题 swift:如何在另一个视图控制器中访问一个变量? - swift: How to take access of one variable in another view controller? 如何从swift中的其他controller访问标签栏高度? - How to access tabbar height from other controller in swift? 如何将数据从一个视图控制器传递到另一个SWIFT - How to pass data from one view controller to the other SWIFT swift:从控制器访问正在使用的场景变量 - swift: Access scene variable in use from controller 从另一个视图控制器访问在一个视图控制器中声明的变量 - Access a variable declared in one view controller from another view controller 如何在一个视图 controller 从另一个视图 controller 访问变量? - How do I access a variable in one view controller from another view controller? 我如何让一个视图控制器从另一个视图控制器访问变量? - How would I have one view controller access a variable from another view controller? 如何在 Swift 中以编程方式从一个控制器导航到另一个视图控制器? - How to navigate from one controller to another View Controller programmatically in Swift? 如何从一个视图控制器导航到另一个 - How to navigate From one view controller to other 从另一个视图控制器访问变量。 迅速 - Access a variable from another view controller. Swift
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM