6

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

当我单击一行时,我会显示一条警报,以警告用户潜在的破坏性行为。我使用了新的 UIAlertController,结果如下: UIAlertViewController 出现...

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

这是我的代码:

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

我错过了什么 ?

谢谢你的帮助

4

2 回答 2

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

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

于 2014-11-06T22:44:02.520 回答
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;
}

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

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

于 2014-09-25T08:30:26.567 回答