简体   繁体   中英

How to display a specific view from the action of the Snackbar in IOS app in SWIFT3 XCODE 8.2

How can I display another view on my IOS app in SWift 3 from the action of Snackbar in IOS? I tried the following code(ie As soon as the snackbar appear and I tap on the action part of the Snackbar then a new view of PatientViewController should open with Id as PatientVC ) :

override func setSelected(_ selected: Bool, animated: Bool) {

        super.setSelected(selected, animated: animated)
        // Configure the view for the selected state
        let message = MDCSnackbarMessage()

        let action = MDCSnackbarMessageAction()
        let actionhandler = {() in
           let actionmessage = MDCSnackbarMessage()
            actionmessage.text = "Button Click Success"
           MDCSnackbarManager.show(actionmessage)
            let storyboard = UIStoryboard(name: "Main", bundle: nil);
            let vc = storyboard.instantiateViewController(withIdentifier: "PatientVC") as! PatientViewController; 
            self.navigationController?.pushViewController(vc, animated: true);



        }
        action.handler = actionhandler
        action.title = "Done"
        message.action = action





        message.text = "Welcome"
        MDCSnackbarManager.show(message)

    }

where PateintVC is the id and PatientViewController is the name of the viewController I want to display.But this is giving me error that EncounterFilterTableViewCell has no member navigationController . And as soon as I am inhereting UIViewController along with the UITableViewCell in my code. It is giving error that multiple inheretence from classes UITableViewCell and UIviewController . What should be done so that I can display my view represented by PatientViewController with ID PatientVC . Below is the complete code of my class where I have my Snackbar code.

import UIKit

import MaterialComponents.MaterialSnackbar

class EncounterFilterTableViewCell: UITableViewCell{

    @IBOutlet weak var filterCheckBox: CheckBox!
    @IBOutlet weak var filterNameTextView: UITextView!

    var filterIndex : [String : Int] = [
        getDataFromAppLanguage("Final") + " " + getDataFromAppLanguage("Diagnosis") : 0,
        getDataFromAppLanguage("Diagnosis") : 1,
        getDataFromAppLanguage("Test") : 2,
        getDataFromAppLanguage("Operation") : 3,
        getDataFromAppLanguage("Drug") : 4,
        getDataFromAppLanguage("Media") : 5,
        getDataFromAppLanguage("FormsEn") : 6,
        getDataFromAppLanguage("PatientEn") + " " + getDataFromAppLanguage("Status") : 7
    ]
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    @IBAction func filterBoxClicked(_ sender: AnyObject) {
        EncounterFilterViewController.updateFilterStatus(filterIndex[filterNameTextView.text]!)
    }


    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        // Configure the view for the selected state
        let message = MDCSnackbarMessage()

        let action = MDCSnackbarMessageAction()
        let actionhandler = {() in
           let actionmessage = MDCSnackbarMessage()
            actionmessage.text = "Button Click Success"
           MDCSnackbarManager.show(actionmessage)
             let storyboard = UIStoryboard(name: "Main", bundle: nil);
        let vc = storyboard.instantiateViewController(withIdentifier: "PatientVC") as! PatientViewController; 
        self.navigationController?.pushViewController(vc, animated: true);            
        }
        action.handler = actionhandler
        action.title = "Done"
        message.action = action





        message.text = "Welcome"
        MDCSnackbarManager.show(message)

    }

}

Change your custom cell to get it work -

override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)

    let componentFrame = CGRect(x: 20, y: 20, width: 40, height: 30)
    toggleSwitch.frame = componentFrame
    toggleSwitch.addTarget(self, action: #selector(SwitchTableViewCell.switchAction), for: .valueChanged)
    self.contentView.addSubview(toggleSwitch);
}

convenience init(style: UITableViewCellStyle, reuseIdentifier: String?, callingView : UIViewController) {

    self.init(style: style, reuseIdentifier: reuseIdentifier)
    callingViewController = callingView
}

override func setSelected(_ selected: Bool, animated: Bool) {
    let message = MDCSnackbarMessage()
    message.text = "The groundhog (Marmota monax) is also known as a woodchuck or whistlepig."

    let action = MDCSnackbarMessageAction()
    let actionHandler = {() in

        let storyBoard = UIStoryboard.init(name: "Main", bundle: nil)
        let vc = storyBoard.instantiateViewController(withIdentifier: "DetailView")
        self.callingViewController?.navigationController?.pushViewController(vc, animated: true)

    }
    action.handler = actionHandler
    action.title = "OK"
    message.action = action

    MDCSnackbarManager.show(message)
}

And In ViewController, initialize cell like this-

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    var cell : SwitchTableViewCell? = tableView.dequeueReusableCell(withIdentifier: "TestIdentifier") as? SwitchTableViewCell
    if(cell == nil)
    {
        cell = SwitchTableViewCell(style: .default, reuseIdentifier: "TestIdentifier", callingView : self)
    }

    cell?.toggleSwitch.isOn = myData1[indexPath.row];

    return cell!;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM