在subView上委派

时间:2017-07-07 13:20:15

标签: ios uitableview ipad swift3 subviews

我有4 UIViewController即vcA,vcB,vcC和vcD。现在,vcB有一个tableView,有两行。并且vcB在vcA上进行了subViewed,其中一半像iPad的设置屏幕。到这里,它的工作正常,但如果我点击vcB的tableView的第一个单元格,我需要vcC应该在另一半的半查看,类似地选择vcB的tableview' s第二个单元格vcD应该是subViewed,vcC应该删除。

这是我的屏幕:

iPad Screen

假设你选择"选择国家",另一个vcC的视图应该是subviewd&通过选择" Notification" vcD应该是subViewed,vcC应该删除。 ,不要担心框架,我会设置。并假设父视图在这里是vcA。

在这里,我尝试使用delegate,但是在子视图delegate上无法正常工作我想,通过NSNotificationCenter它可能是可能的,但有没有其他方法可以做到这一点?

谢谢任何帮助将不胜感激。

更新

我按照以下方式获取结果。但我知道这不是正确的方式,也没有发生我正在寻找的东西。

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)

            }


        }
}

1 个答案:

答案 0 :(得分:0)

你分配了'iPadVc'而没有提示你正试图添加countryVC view.it是错误的。您刚刚分配了vc,但vc view在您添加时将为nil

我的建议:将vcA对象引用传递给vcB,同时将vcB.view添加到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)
     }
    }
  }

}
相关问题