2017-10-02 1 views
0

Was habe ich?: Ein View-Controller (WhereViewController) mit einigen "Kind/Container" -Ansichten. Eine davon ist eine Sammlungsansicht (typeCollectionView). die WhereViewController in einem Navigation eingebettet: ScreenshotMöchten Sie aus einer Containeransicht zu einem anderen View-Controller wechseln

class WhereViewController: UIViewController { 

lazy var progressContainerView: ProgressBarView = { 
    let containerView = ProgressBarView(frame: CGRect(x: 0, y: 0, width: 0, height: 0)) 

    containerView.translatesAutoresizingMaskIntoConstraints = false 

    return containerView 
}() 

let questionLabel: UILabel = { 
    let label = UILabel() 

    label.text = "question?" 
    label.textAlignment = NSTextAlignment.center 
    label.font = UIFont.init(name: "OpenSans-Italic", size: 14) 
    label.textColor = UIColor(red:0.33, green:0.33, blue:0.33, alpha:1.0) 
    label.translatesAutoresizingMaskIntoConstraints = false 

    return label 
}() 

let typeCollectionView: WhereCollectionView = { 
    let collectionView = WhereCollectionView(frame: CGRect(x: 0, y: 0, width: 0, height: 0)) 

    collectionView.translatesAutoresizingMaskIntoConstraints = false 

    return collectionView 
}() 

override func viewDidLoad() { 
    super.viewDidLoad() 

    navigationItem.title = "Schaden melden" 

    view.backgroundColor = UIColor.white 


    view.addSubview(progressContainerView) 
    view.addSubview(questionLabel) 
    view.addSubview(typeCollectionView) 

    setupProgressContainerView() 
    setupQuestionLabel() 
    setupTypeCollectionView() 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 

private func setupProgressContainerView() { 
    progressContainerView.topAnchor.constraint(equalTo: view.topAnchor, constant: 60).isActive = true 
    progressContainerView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true 
    progressContainerView.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true 
    progressContainerView.heightAnchor.constraint(equalToConstant: 50).isActive = true 
} 

private func setupQuestionLabel() { 
    questionLabel.topAnchor.constraint(equalTo: progressContainerView.bottomAnchor, constant: 22).isActive = true 
    questionLabel.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true 
    questionLabel.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true 
    questionLabel.heightAnchor.constraint(equalToConstant: 20).isActive = true 
} 

private func setupTypeCollectionView() { 
    typeCollectionView.topAnchor.constraint(equalTo: questionLabel.bottomAnchor, constant: 20).isActive = true 
    typeCollectionView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true 
    typeCollectionView.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true 
    typeCollectionView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true 
} 

}

Wollen versuche ich zu tun: Im typeCollectionView: Wenn ein Element ausgewählt ist (didSelectItemAt) Ich möchte auf die nächste schieben viewController über navigationController des WhereViewControllers. Mein typeCollectionView wie folgt aussieht:

class WhereCollectionView: UICollectionView, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource { 

var whereViewController: WhereViewController? 

let label = ["x", "y", "z"] 

override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) { 
    let layout = UICollectionViewFlowLayout() 
    super.init(frame: CGRect.zero, collectionViewLayout: layout) 

    backgroundColor = UIColor.white 

    delegate = self 
    dataSource = self 

    self.register(WhereCollectionViewCell.self, forCellWithReuseIdentifier: "WhereCollectionViewCell") 
} 

required init?(coder aDecoder: NSCoder) { 
    fatalError("init(coder:) has not been implemented") 
} 

func numberOfSections(in collectionView: UICollectionView) -> Int { 
    // #warning Incomplete implementation, return the number of sections 
    return 1 
} 


func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
    // #warning Incomplete implementation, return the number of items 
    return label.count 
} 

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "WhereCollectionViewCell", for: indexPath) as! WhereCollectionViewCell 

    cell.injureLabel.text = label[indexPath.row] 

    return cell 
} 

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 
    return CGSize(width: self.frame.width, height: 75) 
} 

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 

} 

}

ich habe versucht, die whereViewController zum typeCollectionView zu geben:

let typeCollectionView: WhereCollectionView = { 
    let collectionView = WhereCollectionView(frame: CGRect(x: 0, y: 0, width: 0, height: 0)) 

    collectionView.translatesAutoresizingMaskIntoConstraints = false 
    collectionView.whereViewController = self 

    return tableView 
}() 

aber erhalten: Wert kann nicht vom Typ zuordnen ‚(NSObject) ->() -> WhereViewController 'um' WhereViewController? '

Könnte mir jemand helfen? oder ist es nicht möglich mit dem navigationController aus der collectionView (containerView) zu "sprechen"?

Ich benutze keine Storyboard

Antwort

0

Sie sollen den View-Controller zu den Sammel Delegatmethoden und nicht die Sammlung Ansicht selbst entsprechen.

Make WhereViewController-UICollectionViewDataSource, UICollectionViewDelegate entsprechen und UICollectionViewDelegateFlowLayout:

class WhereViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout 

In WhereViewController ‚s viewDidLoad Methode, die Delegierten und die Datenquelle ein:

override func viewDidLoad() { 
    super.viewDidLoad() 

    typeCollectionView.dataSource = self 
    typeCollectionView.delegate = self 

    [...] 
} 
+0

hilft ich den Code implementiert haben, an: „(typeCollectionView.collectionViewLayout wie? UICollectionViewFlowLayout)? Delegate = self "Ich habe einen Fehler:" Wert des Typs 'UICollectionViewFlowLayout' hat kein Mitglied 'Delegate' ". –

0

Hier können Sie verwenden delegieren Nachricht senden von CollectionView zu ViewController.

Einfach ein Protokoll

protocol CustomDelegate: class { 
    func collectionView(DidSelect : Int) 
} 

Danach erklärt eine Instanz von CustomDelegate Protokoll macht in WhereCollectionView Klasse

class WhereCollectionView: UICollectionView { 

weak var customDelegate:CustomDelegate? 

} 

und in didSelect Methoden Feuer der delegierte Methode

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 
    if let _del = customDelegate { 
     _del.collectionView(DidSelect : IndexPath.row) 
} 

} 

In ViewController Dort Sie hinzufügen collectionview in viewcontroller gesetzt delegate zu self

override func viewDidLoad() { 
    super.viewDidLoad() 

    navigationItem.title = "Schaden melden" 

    view.backgroundColor = UIColor.white 

    typeCollectionView.customDelegate = self 

    view.addSubview(progressContainerView) 
    view.addSubview(questionLabel) 
    view.addSubview(typeCollectionView) 

    setupProgressContainerView() 
    setupQuestionLabel() 
    setupTypeCollectionView() 
} 

Auch die Methode Custom Delegat Implementieren in ViewController

func collectionView(DidSelect : Int) { 
    /// from there you can do your job 
} 
+0

Ich versuche, Ihren Code zu folgen. An dieser Stelle: schwach var delegieren: CustomDelegate? Ich habe ein Fehler, der besagt: "Property 'delegate' mit dem Typ 'CustomDelegate?' Kann eine Eigenschaft mit dem Typ 'UICollectionViewDelegate?' nicht überschreiben? "Könnten Sie helfen? –

+0

Sie erhalten diesen Fehler UICollectionView hat bereits eine Delegate-Variable. Ändern Sie einfach den Namen der CustomDelegate-Variable, damit Ihr Problem gelöst wird. –

0

Versuchen:

1) In Erweiterung:

extension UIView { 

    var parentViewController: UIViewController? { 
     var parentResponder: UIResponder? = self 
     while parentResponder != nil { 
     parentResponder = parentResponder!.next 
     if parentResponder is UIViewController { 
      return parentResponder as! UIViewController! 
     } 
     } 

     return nil 
    } 
} 

2) verwenden Sie können nun:

parentViewController?.present(<ControllerName>, animated: true) 

Hoffe, dass es

Verwandte Themen