简体   繁体   中英

How to present action sheet form a View Controller that present modally?

I have a ViewControllerA that already show as pop out,which is present modally. Inside this ViewControllerA have a tableview with tableViewCell .So in each cell have a button .When users click on this button ,I want to show actionSheet at the bottom of the screen.

Here is my code:

TableViewCell class ,here I connect the button to the IBAction

protocol MyDelegate: class {
    func showDropDownMenu()
}

class tableCell: UITableViewCell {

    weak var delegate: MyDelegate?

    @IBAction func dropDownButton(_ sender: Any) {
        print("print something")
        self.delegate?.showDropDownMenu()
    }

}

ViewControllerA

class ViewControllerA: UIViewController , UITableViewDataSource, UITableViewDelegate,MyDelegate {

func showDropDownMenu() {

    let actionSheet = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)

    // Create your actions - take a look at different style attributes
    let hideAction = UIAlertAction(title: "Hide", style: .default) { (action) in
         print("didPress hide")      
     }

    let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (action) in
       print("didPress cancel")
    }


     actionSheet.addAction(hideAction)
     actionSheet.addAction(cancelAction)

     self.present(actionSheet, animated: true, completion: nil)
}

Suppose when click on the button will call the function showDropDownMenu() in ViewControllerA .Now I click on dropDownButton it will show print something in console(means no problem with the button),but the actionSheet not show up on the bottom.

I not sure what is the problem here,but I suspect is cause by ViewControllerA is present using segue with properties like so:

Kind: Present modally ,Presentation: Over Current Context ,Transition: Cover Vertical

If this is the reason,please help me how to present an actionsheet from a View Controller that presented modally. Thank you

Code for showing ViewControllerA :

func showViewControllerA(Id: Int) {
   self.performSegue(withIdentifier: "showViewControllerA", sender: Id)
}

Refer this link. Though it's for IPad, it will give you a brief idea of where and how to present the action sheet

https://medium.com/@nickmeehan/actionsheet-popover-on-ipad-in-swift-5768dfa82094

I have faced a similar scenario of what you are trying to achive, and this solution which i provided above helped me out. Hope, it helps you out as Well.

Make sure you have set value for delegate . For example

cell.delegate = self;

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