在iOS8中的Popover中呈现UIAlertController

Fré*_*dda 6 uipopover ios8 uialertcontroller

我将UITableViewController设置为在iPad上的弹出框中显示: 弹出窗口内的UITableViewController

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

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

这是我的代码:

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)
    }
}
Run Code Online (Sandbox Code Playgroud)

我错过了什么 ?

谢谢你的帮助

Fré*_*dda 9

找到了 !如果此AlertController出现在弹出窗口内,它必须提供位置信息,sourceView和sourceRect,或barButtonItem.

喜欢

resetWarning.popoverPresentationController?.sourceView = selectedCell?.contentView
resetWarning.popoverPresentationController?.sourceRect = selectedCell!.contentView.frame
Run Code Online (Sandbox Code Playgroud)

我的代码必须看起来像这样:

    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)
    }
}
Run Code Online (Sandbox Code Playgroud)

现在图像看起来像这样: 在此输入图像描述


lxm*_*l56 5

我遇到了同样的问题,无法弄清楚如何防止弹出窗口调整大小。使用警报而不是操作表也会导致弹出窗口调整大小。我发现的解决方法是通过将模态演示样式设置为 来使用操作表作为弹出框本身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;
}
Run Code Online (Sandbox Code Playgroud)

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

我只是将其作为 AppDelegate 中的实用方法,这样我就可以从所有弹出窗口中调用它。这可行,但并不是真正理想的解决方案,因为如果某些原因导致在其中一个弹出窗口启动时触发警报,则它可能会调整大小。如果您弄清楚如何防止发生大小调整,请告诉我。祝你好运!