简体   繁体   English

如何将第二个动作(segue)添加到UIAlertAction动作?

[英]How do I add a second action (segue) to a UIAlertAction action?

Please excuse me, i'm relatively new to ios coding: 请原谅,我对ios编码比较陌生:

I have implemented an actionSheet with "Cancel" and "Buy" buttons. 我已经通过“取消”和“购买”按钮实现了一个actionSheet。 The Buy button action executes successfully, no problem. “购买”按钮操作成功执行,没有问题。

But, what I want to do, is add another (2nd) action that lets user automatically segue to another screen after the original (1st) action is called. 但是,我想做的是添加另一个(第二个)操作,该操作使用户可以在调用原始(第一个)操作后自动选择另一个屏幕。

Simplified: I want to press the Buy button (on my actionSheet) which downloads product, and then want to be automatically taken to the download screen to see my new downloads. 简化:我想按下“下载”按钮(在我的actionSheet上),该按钮下载产品,然后希望自动进入下载屏幕以查看我的新下载内容。

private func setBuyButton() -> Void {

    // Buy button action sheet
    actionSheet = UIAlertController(title: "CONFIRM PURCHASE", message: "\(self.products.title) will be added to your Download page", preferredStyle: .actionSheet)

    let cancel = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)

    let confirmPurchase = UIAlertAction(title: "Buy", style: .default) { action in

        var productList = [String: Bool]()
        let productSetName = [self.productSetId : true]

        self.product.map({ (drop: Drop) -> Void in
            productList[product.productKey] = true
        })

        if let userId = KeychainWrapper.standard.string(forKey: KEY_UID) {
            let downloads = ["product": productList, "productset": productSetName]
            DataService......blah, blah, blah...

        }
    }

    actionSheet.addAction(confirmPurchase)
    actionSheet.addAction(cancel)
}

In your storyboard, control click the yellow circle icon above the view controller and drag to the destination view controller to create a segue. 在情节提要中,按住Control键单击视图控制器上方的黄色圆圈图标,然后拖动到目标视图控制器以创建序列。 创建segue Select the segue and give it an identifier 选择segue并为其指定一个标识符 给你的segue一个标识符

Now you can call performSegue(withIdentifier:sender:) in your first view controller to segue programatically. 现在,您可以在第一个视图控制器中调用performSegue(withIdentifier:sender:)以编程方式进行搜索。 In your case, put 就你而言

self.performSegue(withIdentifier: "yourCustomSegue", sender: nil) 

in your row action handler will do the trick. 在您的行中,动作处理程序将解决问题。

With a programmatically. 以编程方式。

Create a file DownloadView with the subclass of UICollectionViewController , then in your action add these code 使用UICollectionViewController的子类创建文件DownloadView ,然后在您的操作中添加以下代码

let layout = UICollectionViewFlowLayout()
let controller = DownloadView.init(collectionViewLayout: layout)
self.navigationController?.pushViewController(controller, animated: true)

Also, if you don't need a collectionView , create a file with subclass of UIViewController and push it like this 另外,如果不需要collectionView ,请使用UIViewController子类创建一个文件,然后像这样推送它

self.navigationController?.pushViewController(DownloadView(), animated: true)

With Storyboard 带情节提要

Add viewController with identifer down and write these code in action handler 与IDENTIFER添加的viewController down ,写在行动处理这些代码

let downloadView = self.storyboard?.instantiateViewController(withIdentifier: "down") as! DownloadView
self.present(downloadView, animated:true, completion:nil)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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