简体   繁体   中英

Delegate on subView

I have 4 UIViewController namely vcA, vcB, vcC and vcD. Now, vcB has a tableView with two rows. And vcB is subViewed on vcA with one half side like iPad's setting screen. Till here, its working fine, but i required if I click on vcB's tableView's first cell, then vcC should be subViewed on other half side, similarly by selecting vcB's tableview's second cell vcD should be subViewed and vcC should remove.

Here is my screen:

iPad屏幕

Suppose u select "Select Country", the other vcC's view should subviewd & similary by selecting "Notification" vcD should be subViewed and vcC should remove. , and don't worry about frame, i will set that. And assume that the parent view is vcA here.

Here, I tried with delegate but on subViews delegate is not working I think, it can be Possible through NSNotificationCenter but is there any other way to do that ?

Thanks Any help will be appreciated.

Updated

Following way I'm using for get the result. But I know its not the correct way, and also not happening what i'm looking for.

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

        print(indexPath.section)
        print(indexPath.row)
        if UIDevice.Display.typeIsLike == UIDevice.DisplayType.ipad {

            let iPadVc = iPadSettingViewController()

            if indexPath.section == 0 && indexPath.row == 0 {

                var sliderVC: SettingsViewController! = nil

                sliderVC = self.storyboard?.instantiateViewController(withIdentifier: "SettingsViewController") as! SettingsViewController

                sliderVC.view.frame = CGRect(x:0, y: 64, width: CGFloat((iPadVc.view.frame.size.width)/2.4), height: CGFloat(iPadVc.view.frame.size.height-64))
                iPadVc.view?.addSubview(sliderVC.view)


            }
            else if indexPath.section == 0 && indexPath.row == 2 {

                var countryVC : CountryViewController! = nil

                countryVC = self.storyboard?.instantiateViewController(withIdentifier: "CountryViewController") as! CountryViewController

                countryVC.view.frame = CGRect(x:CGFloat((iPadVc.view.frame.size.width)/2.4), y: 64, width: CGFloat((iPadVc.view.frame.size.width) - (iPadVc.view.frame.size.width)/2.4), height: CGFloat(iPadVc.view.frame.size.height-64))

                iPadVc.view?.addSubview(countryVC.view)

            }


        }
}

You allocated 'iPadVc' and without presenting you are trying to add the countryVC view.it's wrong. You just allocated the vc but the vc view will be nil at the time of you are adding.

My suggestion : Pass the vcA object reference to vcB while adding a vcB.view to vcA.view

class vcA: UIViewController {

   func loadSlideVC() {
     viewcontrollerB = //Initialize the vcB 
     viewControllerB.containerVC = self
     //Add the view to vaA
   }
}

class vcB: UIViewController {
  weak var containerVC: vcA?

  func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  if UIDevice.Display.typeIsLike == UIDevice.DisplayType.ipad {
     if indexPath.section == 0 && indexPath.row == 0 {

        var sliderVC: SettingsViewController! = nil
        sliderVC = self.storyboard?.instantiateViewController(withIdentifier: "SettingsViewController") as! SettingsViewController
        containerVC?.view?.addSubview(sliderVC.view)
     }
    }
  }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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