如何使用SWRevealViewController通过委托将数据从Rear_View传递到Front_view?

时间:2017-01-17 10:12:42

标签: ios swift delegates uicollectionview swrevealviewcontroller

我有一个SWRevealViewController,我的Front_View中的UITableView和Rear_View中的UICollectionView。因此,当我点击Front_View表格单元格时,我的Rear_View将打开,我的要求是当我点击Rear_View集合单元格然后我想要传递索引并且控制必须进入Front_View。

我的意思是根据我的rear_view Collection Cell didSelect动作,数据会填充在我的front_view中。我无法将数据从rear_View传递到front_view。我试过委托,但使用SWReveal我无法发送数据。 通常我知道如何通过委托传递数据。

这是我的代码:

前视图:

//Cell For Row At IndexPath
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    //Assign Cell
    let cellIdentifier:String = "SelectCategory"
    var cellFeeds:SelectCategory? = tableView.dequeueReusableCell(withIdentifier: cellIdentifier) as? SelectCategory

    if (cellFeeds == nil)
    {
        var nib:Array = Bundle.main.loadNibNamed("SelectCategory", owner: self, options: nil)!
        cellFeeds = nib[0] as? SelectCategory
    }

    cellFeeds?.selectionStyle = .none


    //For Two cells rear View will open
    if (indexPath.row == 0 || indexPath.row == 1)
    {
        //self.revealViewController().rightViewRevealWidth = 0.0
        self.revealViewController().rightViewRevealOverdraw = 0.0
        self.revealViewController().bounceBackOnOverdraw = false

        cellFeeds?.btnDropDown.addTarget( self.revealViewController(), action:#selector(SWRevealViewController.rightRevealToggle(_:)) , for: UIControlEvents.touchUpInside)

        self.view.addGestureRecognizer(self.revealViewController().tapGestureRecognizer())
    }

    return cellFeeds!
}

//Table view row select method
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    //Assign Cell
    let cellIdentifier:String = "SelectCategory"
    let cellFeeds:SelectCategory? = tableView.dequeueReusableCell(withIdentifier: cellIdentifier) as? SelectCategory

    if(0 == indexPath.row || 1 == indexPath.row)
    {
        //Open Rear View.
        revealViewController().rightRevealToggle(animated: true)
    }
}  


//Delegate of Rear View
extension AddProductView : didTapOnSubCategory {
  func tapOnCategory(index: Int) {
      print("Index Tap is :-  \(index)")
  }
}  


//Declare the delegate
override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
   let vc = GetAddProductDataView()
    vc.delegate = self

    tableView.reloadData()
}  

Rear_View: -

//Declare protocol.
protocol didTapOnSubCategory {
  func tapOnCategory(index : Int)
}  

class GetAddProductDataView: UIViewController,SWRevealViewControllerDelegate {

  //TableView
  @IBOutlet var tableView: UITableView!

  //Collection View
  @IBOutlet var collectionView: UICollectionView!

  //Category Label
  @IBOutlet var categoryLabel: NSLayoutConstraint!

  //Declare Delegate
  var delegate : didTapOnSubCategory?

  //View Did Load
  override func viewDidLoad() {
      super.viewDidLoad()

    //Add Gesture to Swipe Reveal
      self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
      revealViewController().delegate = self

    //Delegate of tableView
    tableView.delegate = self
    tableView.dataSource = self

    collectionView.delegate = self
    collectionView.dataSource = self

    //tableView.isHidden = false
    collectionView.isHidden = true
  }  
  //Select the CollectionView Cell
  func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

     delegate?.tapOnCategory(index: indexPath.row)

     let gotoFrontView = self.storyboard?.instantiateViewController(withIdentifier: "SWAddProductView") as! SWRevealViewController

    revealViewController().setFront(gotoFrontView, animated: true)
    revealViewController().frontViewPosition = FrontViewPosition.left
   }
}

所以在我的front_View和rear_view中你可以看到一切都很好,但是我无法将index_View(类GetAddProductDataView)中的索引传递给front_View。

0 个答案:

没有答案