如何在SWIFT3 XCODE 8.2中的IOS应用程序中的Snackbar的操作中显示特定视图

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在SWIFT3 XCODE 8.2中的IOS应用程序中的Snackbar的操作中显示特定视图相关的知识,希望对你有一定的参考价值。

如何从ios中Snackbar的操作在SWift 3中显示我的IOS应用程序上的另一个视图?我尝试了下面的代码(即一旦小吃栏出现,我点击Snackbar的动作部分,然后PatientViewController的新视图应该以Id为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)

    }

其中PateintVC是id,PatientViewController是我要展示的viewController的名字。但是这给了我EncounterFilterTableViewCell没有成员navigationController的错误。一旦我在我的代码中与UIViewController一起使用UITableViewCell。它给出了来自UITableViewCellUIviewController类的多重遗嘱的错误。应该做什么,以便我可以用ID PatientViewController显示我的PatientVC代表的视图。下面是我班级的完整代码,其中包含我的Snackbar代码。

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)

    }

}
答案

更改自定义单元格以使其正常工作 -

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)
}

在ViewController中,像这样初始化单元格 -

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!;
}

以上是关于如何在SWIFT3 XCODE 8.2中的IOS应用程序中的Snackbar的操作中显示特定视图的主要内容,如果未能解决你的问题,请参考以下文章

构建在 iTunes 中不可见与 xcode 8.2 ios 10 连接

在 XCode 8.2 / Swift 3.0 中更改状态栏样式(没有“查看基于控制器的状态栏外观”)

如何在 Xcode 8/Swift 3 中创建 iOS liveView [重复]

[转]iOS 10.2 XCode 8.2 证书申请 远程推送 打包上架

Xcode 8 / Swift 3 中的 iOS 异步单元测试(waitForExpectations 失败)

Swift3 中的 imageWithSize