Alper Maraz
Alper Maraz

Reputation: 139

pass data from a TableView to the contained Views of a PageViewController

I have a tableview with a list of names and what I wish to have is when pressing one of the cells inside the tableview to pass the name content of the cell to an upcoming page view controller containing 2 different views. I am using a library called pulley in which I am not able to do segues by pushing modally so inside of the 'didSelectRow' method I am doing this

let pagevc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "PageVC") as! PageViewController

(parent as? PulleyViewController)?.setDrawerContentViewController(controller: pagevc, animated: true)

to accomplish the segue to the pageviewcontroller. This should be right. But the 2 Views inside the pageviewcontroller View, they all need the Name of the selected cell. So I did this:

pagevc.name = selBarName

and to be sure i did the same for the 2 views of the page controller like this(still inside of didSelectRow method of the previous tableview class):

let detvc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "DetailVC") as! DetailVC
    detvc.barname = selBarName
    
    let speisevc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "SpeisekarteVC") as! SpeisekarteVC
    
    speisevc.barname = selBarName

but printing the var barname in each class shows that the var is still an empty string. Only the var name of pagevc has changed to the wanted Name. So I instantiated the two views inside viewdidload of the PageViewController Class but it still doesn't pass the data from the pagevc to the other 2 views contained in the pagevc.

I hope I could make the problem clear. If you need more input tell me what is missing. Basically I just need to know how to pass a String from the PageViewController to its contained Views. By now i am able to pass the name of the selected cell to the pageviewcontroller but not to the views that are presented by the pageviewcontroller.

Edit:

The PageController itself should be correct because using just 2 empty views inside it which doesn't require the var name works all fine.

Here is part of the class PageViewController just to make things complete:

class PageViewController: UIPageViewController, UIPageViewControllerDelegate, UIPageViewControllerDataSource {
    
    var name = ""
    
    var pageControl = UIPageControl()
    
    
    
    lazy var orderedViewControllers: [UIViewController] = {
        return [self.newVc(viewController: "DetailVC"),
                self.newVc(viewController: "SpeisekarteVC")]
    }()
    
    override func viewDidLoad() {
        print(name, "hierpageview") //THIS NAME IS PRINTED CORRECTLY AS WISHED
        super.viewDidLoad()
        self.dataSource = self
        self.delegate = self
        
        
        let speisevc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "SpeisekarteVC") as! SpeisekarteVC
        
        speisevc.barname = name
       
        let detvc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "DetailVC") as! DetailVC
        detvc.barname = name
        print(detvc.barname, "dhdhfhdah") //THIS ONE IS ALSO PRINTED RIGHT BUT PRINTING IT INSIDE THE ACTUAL CLASS RETURNS AGAIN AN EMPTY STRING !!!
        
        
        if let firstViewController = orderedViewControllers.first {
            setViewControllers([firstViewController],
                               direction: .forward,
                               animated: true,
                               completion: nil)
            
        }
     
     func newVc(viewController: String) -> UIViewController {
        return UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: viewController)
    }
    
    
  
    func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
        guard let viewControllerIndex = orderedViewControllers.index(of: viewController) else {
            return nil
        }
        
        let previousIndex = viewControllerIndex - 1
        
        
        guard previousIndex >= 0 else {
          
             return nil
        }
        
        guard orderedViewControllers.count > previousIndex else {
            return nil
        }
        
        return orderedViewControllers[previousIndex]
    }
    
    func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
        guard let viewControllerIndex = orderedViewControllers.index(of: viewController) else {
            return nil
        }
        
        let nextIndex = viewControllerIndex + 1
        let orderedViewControllersCount = orderedViewControllers.count
       
        guard orderedViewControllersCount != nextIndex else {
          
             return nil
        }
        
        guard orderedViewControllersCount > nextIndex else {
            return nil
        }
        
        return orderedViewControllers[nextIndex]
    }
    
    
}   
      
    }

Upvotes: 0

Views: 224

Answers (1)

Ashish Bahl
Ashish Bahl

Reputation: 1493

You can do this by :

  • Create a string in your destination root controller. (say for example let barName : String = "")
  • In your didSelectRow method, store the data into the string against your desired indexPath. (say for ex., view.barName //name of your string = // value you want to store, for example if it is an array then maybe myArr[indexPath.row])
  • Then in your destinationViewController, in viewDidLoad method, push value onto the desired label. (say for ex., yourLbl.text = barName)

Upvotes: 0

Related Questions