Swift - 通过Nav Controller进行委派

时间:2017-05-01 03:09:57

标签: ios swift delegates segue unwind-segue

我将在此包含我的完整代码,但我会尝试指出相关位的位置。基本上我正在使用一些新数据从Unwind Segue返回视图控制器。我在'NBARotoHome'VC中成功使用了这些数据,但我还需要通过嵌入式Nav控制器将一些数据传递给'NBARotoTabPager'vc。

我试图使用'UpdateChildView'委托(在第一个代码块的顶部)并在'loadViewData()中调用它的方法(在第一个块底部附近的'if语句')

protocol UpdateChildView : class {
 func updateView()
 func test()
 var playerSelected: Player? { get set }
}



class RotoViewRoundCell: UITableViewCell{

@IBOutlet weak var categoryLabel: UILabel!

}

class RotoViewRoundHeader: UITableViewCell{

}


class NBARotoHome: UIViewController{


@IBOutlet weak var posPaidLabel: UILabel!
@IBOutlet weak var progressIndicator: UIProgressView!
@IBOutlet weak var vsLabel: UILabel!
@IBOutlet weak var fantasyFundsAmountLabel: UILabel!
@IBOutlet weak var fantasyFundsLabel: UILabel!
@IBOutlet weak var playerName: UILabel!
@IBOutlet weak var roundIndicator: UILabel!


var selectedPlayer: Player!

var firstNavController: UINavigationController!

weak var updateChildView : UpdateChildView?

override func viewDidLoad() {
    super.viewDidLoad()

    loadViewData()

    firstNavController = self.navigationController



    let rightBarButton = UIBarButtonItem(title: "Select", style: UIBarButtonItemStyle.plain, target: self, action: #selector(myRightSideBarButtonItemTapped(_:)))
    self.navigationItem.rightBarButtonItem = rightBarButton
    self.title = "NBA Roto"


}


func myRightSideBarButtonItemTapped(_ sender:UIBarButtonItem!){

    performSegue(withIdentifier: "ShowDraft", sender: nil)
}


override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    if segue.identifier == "ShowDraft" {
        let navVC = segue.destination as? UINavigationController
        let nbaDraftList = navVC?.viewControllers.first as! NBADraftList
        nbaDraftList.mainNavController = firstNavController
    }


    if (segue.identifier == "buyNavControllerChild"){

//            let navVC = segue.destination as? UINavigationController
//            let buyVC = navVC?.viewControllers.first as! NBARotoTabPager
//            buyVC.delegate = self

    }


}


override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)


}

@IBAction func prepareForUnwind(segue: UIStoryboardSegue) {



}

func loadViewData(){

    if((selectedPlayer) != nil){
        roundIndicator.text = "Upcoming: " + selectedPlayer.game_time
        playerName.text = selectedPlayer.Name
        vsLabel.text = selectedPlayer.visiting + " @ " + selectedPlayer.home
        fantasyFundsLabel.text = ""
        fantasyFundsAmountLabel.text = ""

          updateChildView?.test()
     //   updateChildView?.playerSelected = selectedPlayer
    //    updateChildView?.updateView()

    }else{
        roundIndicator.text = "Select a Player"
        playerName.text = "No Player Selected"
        vsLabel.text = "--"
        fantasyFundsLabel.text = "Fantasy Funds"
        fantasyFundsAmountLabel.text = "$10,000"

    }

}

}

因为我无法让代表工作,所以我一直在用上面的'prepare'方法设置其委托属性-'buyVC.delegate = self' - 但我得到'buyVC没有成员代表'所以这是一个死胡同。

下一段代码是嵌入在导航控制器中的NBARotoTabPager vc。由于我不再确定的原因,我决定将它作为NBARotoHome的子类,但它基本上是一个自定义标签页面管理器,它使用分段控件在两个额外的vcs之间切换。

此时最重要的一步就是让“测试”功能正常工作(只打印'test'。它在下面的代码块中实现,位于updateView上方的第二位)。

class NBARotoTabPager: NBARotoHome, UpdateChildView{

@IBOutlet weak var segmentedControl: UISegmentedControl!
@IBOutlet weak var scoreKey: UIBarButtonItem!
@IBOutlet weak var standings: UIBarButtonItem!

 var playerSelected: Player?


override func viewDidLoad() {



    navigationController?.navigationBar.barTintColor = UIColor(red: 27/255, green: 27/255, blue: 27/255, alpha: 1)


    scoreKey.setTitleTextAttributes([NSFontAttributeName: UIFont(name: "Helvetica", size: 13.0)!], for: UIControlState.normal)
    scoreKey.tintColor = UIColor.blue

    standings.setTitleTextAttributes([NSFontAttributeName: UIFont(name: "Helvetica", size: 13.0)!], for: UIControlState.normal)
    standings.tintColor = UIColor.blue

    setupView()
}

private func setupView() {
    setupSegmentedControl()
    updateView()
}

private func setupSegmentedControl() {
    // Configure Segmented Control
    segmentedControl.removeAllSegments()
    segmentedControl.insertSegment(withTitle: "Live", at: 0, animated: false)
    segmentedControl.insertSegment(withTitle: "Avg / +", at: 1, animated: false)
    segmentedControl.addTarget(self, action: #selector(selectionDidChange(_:)), for: .valueChanged)
    segmentedControl.selectedSegmentIndex = 0
}

func selectionDidChange(_ sender: UISegmentedControl) {
    updateView()
}


private lazy var viewLiveTab: NBARotoLive = {
    // Load Storyboard
    let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)

    // Instantiate View Controller
    var viewController = storyboard.instantiateViewController(withIdentifier: "NBARotoLive") as! NBARotoLive


    if((self.playerSelected) != nil){
        viewController.selectedPlayer = self.playerSelected
    }


    // Add View Controller as Child View Controller
    self.add(asChildViewController: viewController)

    return viewController
}()

private lazy var viewAvgsTab: NBARotoAvgs = {
    // Load Storyboard
    let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)

    // Instantiate View Controller
    var viewController = storyboard.instantiateViewController(withIdentifier: "NBARotoAvgs") as! NBARotoAvgs

    if((self.playerSelected) != nil){
        viewController.selectedPlayer = self.playerSelected
    }


    // Add View Controller as Child View Controller
    self.add(asChildViewController: viewController)

    return viewController
}()

private func add(asChildViewController viewController: UIViewController) {
    // Add Child View Controller
    addChildViewController(viewController)

    // Add Child View as Subview
    view.addSubview(viewController.view)

    // Configure Child View
    viewController.view.frame = view.bounds
    viewController.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]

    // Notify Child View Controller
    viewController.didMove(toParentViewController: self)
}

private func remove(asChildViewController viewController: UIViewController) {
    // Notify Child View Controller
    viewController.willMove(toParentViewController: nil)

    // Remove Child View From Superview
    viewController.view.removeFromSuperview()

    // Notify Child View Controller
    viewController.removeFromParentViewController()
}

internal func test(){
    print("test")
}

internal func updateView() {

    if segmentedControl.selectedSegmentIndex == 0 {



        let position = viewAvgsTab.tableView.contentOffset.y;
        viewLiveTab.tableView.contentOffset = CGPoint(x:0, y:position);


        remove(asChildViewController: viewAvgsTab)
        add(asChildViewController: viewLiveTab)

    } else {

        let position = viewLiveTab.tableView.contentOffset.y;
        viewAvgsTab.tableView.contentOffset = CGPoint(x:0, y:position);

        remove(asChildViewController: viewLiveTab)
        add(asChildViewController: viewAvgsTab)
    }
}

}

我看了很多例子,但我不明白整个'设置委托'的事情,即theSecondViewController.delegate = self。有时我会看到您不需要这样做的示例。有时候,我的风险投资公司似乎甚至没有委托财产。所以我不确定这是否是我的具体问题,但任何方向都会非常感激。谢谢

1 个答案:

答案 0 :(得分:2)

实施委托有三个步骤。

  1. 创建协议..(您已经通过创建updateChildView协议来完成此任务)
  2. 你需要在你希望收到的类中实现这个协议并处理这些数据..(你没有完成这一步,这就是为什么你不能设置buyVC.delegate = self)
  3. 你需要在ViewController2中添加一个名为“delegate”的属性,并在步骤1中将它作为你的协议类型(你没有完成这一步,vc2中没有名为“delegate”的属性..这就是为什么你得到这个错误'buyVC没有成员代表')
  4. 这是一个简单的例子:

    协议:

    protocol UpdateChildView{ //removed :class
      func updateView()
      func test()
      var playerSelected: Player? { get set }
    }
    

    Viewcontroller A:

    class NBARotoHome: UIViewController, UpdateChildView { //added conformance to the protocol
    
      //add the methods for conforming to protocol and add your implementation
    
       func updateView() {
            //add your implementation
       }
       func test(){
            //add your implementation
       }
       var playerSelected: Player? {
            //add your implementation
       }
    
      prepare(for: Segue) {
        /** code for passing data **/
        let navVC = segue.destination as? UINavigationController
        let buyVC = navVC?.viewControllers.first as! NBARotoTabPager
        buyVC.delegate = self
                //sets the delegate in the new viewcontroller 
                //remember.. delegate is a property in the next vc
               // and the property only accepts any viewcontroller that is implements updatechildview protocol
        present(vc2)
        }
      } 
    

    viewcontroller2:

    class viewControllerB: UIViewController {
    
      var delegate: UpdateChildView? //add this
    
      viewdidload {
          delegate?.test() //call the func in the previous vc
      }
    }