在scrollview ios swift中添加viewcontrollers

时间:2016-12-26 10:28:57

标签: ios xcode uitableview uiscrollview xcode8

我有滚动视图的baseview控制器。在滚动视图中我添加了三个viewcontrollers视图(xib)。所以我可以在horizantal中滚动这三个。在我的第一个视图控制器中我显示一个tablevieew.While运行我的应用程序,它显示所有我的3个视图控制器。但是,当我触摸我的tableview单元格消失了。我在xcode 7.3中做过。它工作正常。但是在xcode 8中它无法正常工作。请帮我解决这个问题。我附上了我的示例代码

let scrollViewWidth:CGFloat = self.scrollContainer.frame.width
        let scrollViewHeight:CGFloat = self.scrollContainer.frame.height
        let x = CGFloat(i) * scrollViewWidth

        if i == 0{
            let qualification = EducationViewController (nibName: "EducationViewController", bundle: nil)
            //qualification.view.frame.size.height = scrollViewHeight
            //qualification.view.frame.size.width = scrollViewWidth
            qualification.view.frame.origin.x = x
            self.scrollContainer!.addSubview(qualification.view)
            qualification.didMove(toParentViewController: self)
        }
        else if i == 1{
            let state = StateRegistrationViewController (nibName: "StateRegistrationViewController", bundle: nil)
            //state.view.frame.size.height = scrollViewHeight
           // state.view.frame.size.width = scrollViewWidth
            state.view.frame.origin.x = x
            self.scrollContainer!.addSubview(state.view)
            state.view.backgroundColor = UIColor.red
            state.didMove(toParentViewController: self)
        }
        else if i == 2{
            let exp = ExperienceViewController (nibName: "ExperienceViewController", bundle: nil)
           // exp.view.frame.size.height = scrollViewHeight
           // exp.view.frame.size.width = scrollViewWidth
            exp.view.frame.origin.x = x
            self.scrollContainer!.addSubview(exp.view)
            exp.view.backgroundColor = UIColor.orange

            exp.didMove(toParentViewController: self)
        }}

2 个答案:

答案 0 :(得分:1)

  1. 启动viewController

    E/AndroidRuntime: FATAL EXCEPTION: main
      Process: com.erlanggastudio.myagri, PID: 11839
      java.lang.RuntimeException: Unable to start activity ComponentInfo{com.erlanggastudio.myagri/com.erlanggastudio.myagri.activities.ActivityCariPestisida}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Spinner.setOnItemSelectedListener(android.widget.AdapterView$OnItemSelectedListener)' on a null object reference
          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2465)
          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2530)
          at android.app.ActivityThread.access$900(ActivityThread.java:163)
          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1358)
          at android.os.Handler.dispatchMessage(Handler.java:102)
          at android.os.Looper.loop(Looper.java:135)
          at android.app.ActivityThread.main(ActivityThread.java:5536)
          at java.lang.reflect.Method.invoke(Native Method)
          at java.lang.reflect.Method.invoke(Method.java:372)
          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1397)
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1192)
       Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Spinner.setOnItemSelectedListener(android.widget.AdapterView$OnItemSelectedListener)' on a null object reference
          at com.erlanggastudio.myagri.activities.ActivityCariPestisida.onCreate(ActivityCariPestisida.java:43)
    
  2. 将viewControllers添加到数组

    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let aViewController = storyboard.instantiateViewController(withIdentifier: "A") as! AViewController;
    let bViewController = storyboard.instantiateViewController(withIdentifier: "B") as! BViewController;
    let cViewController = storyboard.instantiateViewController(withIdentifier: "C") as! CViewController;
    
  3. 运行for循环,将viewControllers添加到scrollView

    let viewControllers = [aViewController, bViewController, cViewController]
    
  4. 全部完成!

答案 1 :(得分:0)

在scrollview上添加自定义viewcontroller的视图时,还应将自定义viewcontroller添加为主控制器的子控制器。

就像在你的情况下你有三个自定义控制器然后根据代码中的if else子句你必须添加:

self.addChildViewController(qualification)
self.addChildViewController(state)
self.addChildViewController(exp)

代码中的各个块。

您的代码将如下:

let scrollViewWidth:CGFloat = self.scrollContainer.frame.width
        let scrollViewHeight:CGFloat = self.scrollContainer.frame.height
        let x = CGFloat(i) * scrollViewWidth

        if i == 0{
            let qualification = EducationViewController (nibName: "EducationViewController", bundle: nil)
            //qualification.view.frame.size.height = scrollViewHeight
            //qualification.view.frame.size.width = scrollViewWidth
            qualification.view.frame.origin.x = x
            self.scrollContainer!.addSubview(qualification.view)
            self.addChildViewController(qualification)
            qualification.didMove(toParentViewController: self)
        }
        else if i == 1{
            let state = StateRegistrationViewController (nibName: "StateRegistrationViewController", bundle: nil)
            //state.view.frame.size.height = scrollViewHeight
           // state.view.frame.size.width = scrollViewWidth
            state.view.frame.origin.x = x
            self.scrollContainer!.addSubview(state.view)
            self.addChildViewController(state)
            state.view.backgroundColor = UIColor.red
            state.didMove(toParentViewController: self)
        }
        else if i == 2{
            let exp = ExperienceViewController (nibName: "ExperienceViewController", bundle: nil)
           // exp.view.frame.size.height = scrollViewHeight
           // exp.view.frame.size.width = scrollViewWidth
            exp.view.frame.origin.x = x
            self.scrollContainer!.addSubview(exp.view)
            self.addChildViewController(exp)
            exp.view.backgroundColor = UIColor.orange

            exp.didMove(toParentViewController: self)
        }}