在iOS8中的Popover中呈现UIAlertController

时间:2014-09-12 09:57:13

标签: ios8 uipopover uialertcontroller

我将UITableViewController设置为在iPad上的弹出框中显示: UITableViewController inside a popover

当我点击一行时,我会显示一条警告,警告用户可能存在破坏性行为。 我使用了新的UIAlertController,接下来会发生什么: UIAlertViewController appears ...

popover变得非常小(事实上是alertController视图的大小)。如果我按取消,我可以看到结果: ... making the popover shrink!

这是我的代码:

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

    var previouslySelectedCell: UITableViewCell?
    if checkedIndexPath != nil {
        previouslySelectedCell = tableView.cellForRowAtIndexPath(checkedIndexPath)
    }
    var selectedCell = tableView.cellForRowAtIndexPath(indexPath)

    let selectedCurrency = PortfolioCurrencyStore.sharedStore().allCurrencies[indexPath.row]

    if selectedCurrency.symbol != GlobalSettings.sharedStore().portfolioCurrency {

        // Warning : changing the portfolio currency will reset the portfolio
        var resetWarning = UIAlertController(title: NSLocalizedString("Currency Picker VC:AS title", comment: "Changing currency will reset portfolio"), message: nil, preferredStyle: .ActionSheet)

        // destructive button
        let resetAction = UIAlertAction(title: NSLocalizedString("Currency Picker VC:AS destructive", comment: "Destructive button title"), style: .Destructive, handler: { (action: UIAlertAction!) in

            // Remove checkmark from the previously marked cell
            previouslySelectedCell?.accessoryType = .None

            // Add checkmark to the selected cell
            selectedCell?.accessoryType = .Checkmark
            self.checkedIndexPath = indexPath

            // Animate deselection of cell
            self.tableView.deselectRowAtIndexPath(indexPath, animated:true)

            // Stock the portfolio currency as NSUserDefaults
            GlobalSettings.sharedStore().portfolioCurrency = selectedCurrency.symbol // link between portfolioCurrency as a String and currency.symbol as the property of a Currency instance.

            // Delete all items from the StockStore
            StockStore.sharedStore().removeAllStocks()
            println("StockStore : all entries were deleted")


            // Reload tableView
            self.tableView.reloadData()

            })

        // cancel button
        let cancelAction = UIAlertAction(title: NSLocalizedString("Currency Picker VC:AS cancel", comment: "Cancel button title"), style: .Cancel, handler:nil)

        resetWarning.addAction(resetAction)
        resetWarning.addAction(cancelAction)

        presentViewController(resetWarning, animated: true, completion: nil)

    } else {
        // Animate deselection of cell
        tableView.deselectRowAtIndexPath(indexPath, animated:true)
    }
}

我错过了什么吗?

感谢您的帮助

2 个答案:

答案 0 :(得分:9)

发现它! 如果此AlertController在弹出框中显示,它必须提供位置信息,可以是sourceView和sourceRect,也可以是barButtonItem。

喜欢

resetWarning.popoverPresentationController?.sourceView = selectedCell?.contentView
resetWarning.popoverPresentationController?.sourceRect = selectedCell!.contentView.frame

我的代码看起来像那样:

    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

    var previouslySelectedCell: UITableViewCell?
    if checkedIndexPath != nil {
        previouslySelectedCell = tableView.cellForRowAtIndexPath(checkedIndexPath)
    }
    var selectedCell = tableView.cellForRowAtIndexPath(indexPath)

    let selectedCurrency = PortfolioCurrencyStore.sharedStore.allCurrencies[indexPath.row]

    if selectedCurrency.symbol != GlobalSettings.sharedStore.portfolioCurrency {

        // Warning : changing the portfolio currency will reset the portfolio
        var resetWarning = UIAlertController(title: NSLocalizedString("Currency Picker VC:AS title", comment: "Changing currency will reset portfolio"), message: nil, preferredStyle: .ActionSheet)

        // destructive button
        let resetAction = UIAlertAction(title: NSLocalizedString("Currency Picker VC:AS destructive", comment: "Destructive button title"), style: .Destructive, handler: { (action: UIAlertAction!) in

            // Remove checkmark from the previously marked cell
            previouslySelectedCell?.accessoryType = .None

            // Add checkmark to the selected cell
            selectedCell?.accessoryType = .Checkmark
            self.checkedIndexPath = indexPath

            // Animate deselection of cell
            self.tableView.deselectRowAtIndexPath(indexPath, animated:true)

            // Stock the portfolio currency as NSUserDefaults
            GlobalSettings.sharedStore.portfolioCurrency = selectedCurrency.symbol // link between portfolioCurrency as a String and currency.symbol as the property of a Currency instance.

            // Delete all items from the StockStore
            StockStore.sharedStore.removeAllStocks()
            println("StockStore : all entries were deleted")

            // Delete all items from the CurrencyRateStore
            CurrencyRateStore.sharedStore.deleteAllRates()
            println("CurrencyStore : all entries were deleted")

            // Delete all items from the SalesJournal
            SalesJournal.sharedStore.removeAllEntries()
            println("SalesJournal : all Sales journal entries were deleted")


            // Reload tableView
            self.tableView.reloadData()

            // On Regular sizes, the currency picker is presented inside a popover : reloadData of the List View
            NSNotificationCenter.defaultCenter().postNotificationName("CurrencyPickerVC_PortfolioCurrencyDidChangeNotification", object:nil, userInfo:nil)

            // Animate deselection of cell
            tableView.deselectRowAtIndexPath(indexPath, animated:true)

            // Return to root VC
            self.navigationController?.popToRootViewControllerAnimated(true)

            })



        // cancel button
        let cancelAction = UIAlertAction(title: NSLocalizedString("Currency Picker VC:AS cancel", comment: "Cancel button title"), style: .Cancel, handler: { (alertAction: UIAlertAction!) -> Void in
            // Animate deselection of cell
            self.tableView.deselectRowAtIndexPath(indexPath, animated:true)
        })

        resetWarning.addAction(resetAction)
        resetWarning.addAction(cancelAction)

        // If this AlertController is presented inside a popover, it must provide the location information, either a sourceView and sourceRect or a barButtonItem.
        resetWarning.popoverPresentationController?.sourceView = selectedCell?.contentView
        resetWarning.popoverPresentationController?.sourceRect = selectedCell!.contentView.frame

        presentViewController(resetWarning, animated: true, completion: nil)


    } else {
        // Animate deselection of cell
        tableView.deselectRowAtIndexPath(indexPath, animated:true)
    }
}

现在图片看起来像这样: enter image description here

答案 1 :(得分:4)

我遇到了同样的问题,无法弄清楚如何防止弹出窗口大小调整。使用警报而不是操作表也会导致弹出窗口调整大小。我找到的解决方法是通过将模态演示样式设置为UIModalPresentationPopover来将操作表用作弹出窗口。我知道你使用的是Swift,但我的代码是Objective-C;希望你能轻松翻译:

- (UIAlertController *)modalAlertWithTitle:(NSString *)title andMessage:(NSString *)message fromViewController:(UIViewController *)sender {
  UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleActionSheet];

  // This will turn the Action Sheet into a popover
  [alertController setModalPresentationStyle:UIModalPresentationPopover];

  // Set Modal In Popover to YES to make sure your popover isn't dismissed by taps outside the popover controller
  [alertController setModalInPopover:YES];

  // Get the PopoverPresentationController and set the source View and Rect so the Action Sheet knows where to pop up
  UIPopoverPresentationController *popPresenter = [alertController popoverPresentationController];
  popPresenter.sourceView = sender.view;
  popPresenter.sourceRect = sender.view.bounds;

  return alertController;
}

记住将取消按钮的UIAlertAction样式设置为默认非常重要。如果将样式设置为“取消”,则它将不会显示在操作表上,因为它使用ModalPresentationPopover。由于我们将ModalInPopover设置为YES,因此用户也无法通过点击操作表外部来取消。将取消按钮的样式设置为默认将确保它显示在工作表上。

我刚刚在我的AppDelegate中将它作为一个实用工具方法,所以我可以从我的所有弹出框中调用它。这可行,但实际上并不是一个理想的解决方案,因为如果某个东西导致警报在你的一个弹出窗口启动时触发,它可能会调整大小。如果您弄清楚如何防止调整大小,请告诉我。祝你好运!