在iOS8中的Popover中呈现UIAlertController

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

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

popover变得非常小(事实上是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)
    }
}

我错过了什么 ?

谢谢你的帮助

解决方法

找到了 !
如果此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",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",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,completion: nil)


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

现在图像看起来像这样:

相关文章

当我们远离最新的 iOS 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...