Present ViewController in UITableViewCell aufrufen

Diese Seite verwendet Cookies. Durch die Nutzung unserer Seite erklären Sie sich damit einverstanden, dass wir Cookies setzen. Weitere Informationen

  • Present ViewController in UITableViewCell aufrufen

    Hallo,

    Ich habe vor einen ViewController über eine Action aus einer UITableViewCell heraus zu öffnen. Nur leider will mir das nicht wirklich gelingen da soweit ich weiß, nicht wie im ViewController bestimmte Funktionen wie present zur Verfügung stehen. Ich habe daraufhin etwas über protocols gelesen und es versucht über diese zu lösen. Leider kriege ich aber immer folgende Fehlermeldung:

    Cannot assign value of type 'ProfileTableViewController' to type 'showSettingsProtocol?'. Es ist wohl irgendein Problem mit den Optionals aber wie komme ich um diese Meldung herum ? Bzw. ist die Methode die ich vorhabe überhaupt am sinnvollsten oder gibt es eventuell noch einen einfachern Weg ?

    Hier der relevante Code:

    Quellcode

    1. import UIKit
    2. protocol showSettingsProtocol {
    3. func showSettings()
    4. }
    5. class ProfileImageTableViewCell: UITableViewCell {
    6. @IBOutlet weak var profileImage: UIImageView!
    7. @IBOutlet weak var profileSettingsButton: UIButton!
    8. var delegate: showSettingsProtocol?
    9. @IBAction func settingsButtonTapped(_ sender: Any) {
    10. if profileSettingsButton.currentTitle == "Settings" {
    11. self.delegate?.showSettings()
    12. } else {
    13. }
    14. }
    15. }
    Alles anzeigen

    und noch die TableView:

    Quellcode

    1. override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    2. if indexPath.section == 0 {
    3. let cell = tableView.dequeueReusableCell(withIdentifier: "UserLine", for: indexPath) as! ProfileImageTableViewCell
    4. cell.delegate = self
    5. [...]
    6. }
    7. func showSettings(){
    8. if let destinationViewController = storyboard?.instantiateViewController(withIdentifier: "settingsController") as? SettingsViewController {
    9. // Present Second View
    10. present(destinationViewController, animated: true, completion: nil)
    11. }
    12. }
    Alles anzeigen
  • Hi,

    hast du deinen ProfileTableViewController auch als Delegate deklariert?

    Quellcode

    1. class ProfileTableViewController: UITableViewController, showSettingsProtocol {
    2. ..
    3. func showSettings() {
    4. ..
    5. }
    6. }


    oder:

    Quellcode

    1. extension ProfileTableViewController: showSettingsProtocol {
    2. func showSettings() {
    3. ..
    4. }
    5. }

    /Update: ich habe mal getestet per Knopfdruck aus einer TableViewCell heraus eine View zu präsentieren und es hat funktioniert. Einfach:

    Quellcode

    1. @IBAction func press(_ sender: Any) {
    2. let vc = storyboard?.instantiateViewController(withIdentifier: "test") as! ViewController
    3. present(vc, animated: true, completion: nil)
    4. }
    Dann bräuchtest du das Protokoll gar nicht. Oder wirft das bei dir einen Fehler?

    Dieser Beitrag wurde bereits 3 mal editiert, zuletzt von Tazaki ()

  • Oh man ich hatte vergessen das ganze in der Class auch als Delegate zu deklarieren. Nachdem ich das gemacht hatte funktionierte es auf anhieb. Vielen Dank :)

    Bei deinem update ist das Problem das ich dann die Meldung bekomme das storyboard? nicht vorhanden ist. Kenne das nämlich auch so. Weiß auch nicht wie es dazu kommt und kann es mir nicht erklären weshalb ich auf protocol ausweichen musste...