在iOS8中的Popover中呈现UIAlertController [英] Present a UIAlertController from within a Popover in iOS8

查看:129
本文介绍了在iOS8中的Popover中呈现UIAlertController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设置一个UITableViewController在iPad上的弹出框中显示:



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



弹出窗口变得非常小(实际上是alertController视图的大小)。如果我按下取消,我可以看到结果:
<这是我的代码:

 覆盖func tableView(tableView:UITableView) ,didSelectRowAtIndexPath indexPath:NSIndexPath){

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

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

如果selectedCurrency.symbol!= GlobalSettings.sharedStore()。portfolioCurrency {

//警告:更改投资组合货币将重置投资组合
var resetWarning = UIAlertController(标题:NSLocalizedString(货币选择器VC:AS标题,评论:更改货币将重置投资组合),消息:nil,preferredStyle:.ActionSheet)

//破坏性按钮
let resetAction = UIAlertAction(标题:NSLocalizedString(货币选择器VC:AS破坏性,评论:破坏性按钮标题),样式:.Destructive,handler:{(action :UIAlertAction!)in

//从上一页删除复选标记标记为单元格
previousSelectedCell?.accessoryType = .None

//为所选单元格添加复选标记
selectedCell?.accessoryType = .Checkmark
self.checkedIndexPath = indexPath

//单元格的动画取消选择
self.tableView.deselectRowAtIndexPath(indexPath,animated:true)

//将投资组合货币作为NSUserDefaults
存货GlobalSettings.sharedStore()。portfolioCurrency = selectedCurrency.symbol //将portfolioCurrency作为String链接,将currency.symbol作为Currency实例的属性进行链接。

//删除StockStore中的所有商品
StockStore.sharedStore()。removeAllStocks()
println(StockStore:所有条目都被删除)


//重新加载tableView
self.tableView.reloadData()

})

//取消按钮
let cancelAction = UIAlertAction(标题:NSLocalizedString(货币选择器VC:AS取消,注释:取消按钮标题),样式:.Cancel,handler:nil)

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

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

} else {
//取消选择单元格
tableView.deselectRowAtIndexPath(indexPath,animated:true)
}
}

我错过了什么吗?



感谢您的帮助

解决方案

找到它!
如果此AlertController出现在一个popover中,它必须提供位置信息,sourceView和sourceRect,或barButtonItem。



喜欢

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

我的代码必须如此:

 覆盖func tableView(tableView:UITableView,didSelectRowAtIndexPath indexPath:NSIndexPath){

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

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

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

//警告:更改投资组合货币将重置组合
var resetWarning = UIAlertController(标题:NSLocalizedString(货币选择器VC:AS标题,评论:更改货币将重置投资组合),消息:nil,preferredStyle:.ActionSheet)

//破坏性按钮
let resetAction = UIAlertAction(标题:NSLocalizedString(货币选择器VC:AS破坏性,评论:破坏性按钮标题),样式:。破坏性,处理程序:{(动作:UIAlertAction!) in

//删除previousl的复选标记y标记单元格
previousSelectedCell?.accessoryType = .None

//为所选单元格添加复选标记
selectedCell?.accessoryType = .Checkmark
self.checkedIndexPath = indexPath

//单元格的动画取消选择
self.tableView.deselectRowAtIndexPath(indexPath,animated:true)

//将投资组合货币作为NSUserDefaults
存货GlobalSettings.sharedStore.portfolioCurrency = selectedCurrency.symbol //将portfolioCurrency作为String链接,将currency.symbol作为Currency实例的属性进行链接。

//删除StockStore中的所有商品
StockStore.sharedStore.removeAllStocks()
println(StockStore:所有条目都被删除)

//从CurrencyRateStore中删除所有项目
CurrencyRateStore.sharedStore.deleteAllRates()
println(CurrencyStore:删除所有条目)

//从SalesJournal中删除所有项目
SalesJournal.sharedStore.removeAllEntries()
println(SalesJournal:所有销售日记帐分录都已删除)


//重新加载tableView
self。 tableView.reloadData()

//在普通尺寸上,货币选择器显示在一个弹出框内:列表视图的reloadData
NSNotificationCenter.defaultCenter()。postNotificationName(CurrencyPickerVC_PortfolioCurrencyDidChangeNotification,对象:nil,userInfo:nil)

/ / Animate取消选择单元格
tableView.deselectRowAtIndexPath(indexPath,animated:true)

//返回根目录VC
self.navigationController?.popToRootViewControllerAnimated(true)

})



//取消按钮
让cancelAction = UIAlertAction(标题:NSLocalizedString(货币选择器VC:AS取消,评论: 取消按钮标题),样式:。取消,处理程序:{(alertAction:UIAlertAction!) - >无效
// Animate取消选择单元
self.tableView.deselectRowAtIndexPath(indexPath,animated:true)
})

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

//如果此AlertController出现在弹出窗口内,它必须提供位置信息,sourceView和sourceRect或barButtonItem。
resetWarning.popoverPresentationController?.sourceView = selectedCell?.contentView
resetWarning.popoverPresentationController?.sourceRect = selectedCell!.contentView.frame

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


} else {
// Animate取消选择单元
tableView.deselectRowAtIndexPath(indexPath,animated:true)
}
}

现在图片如下所示:


I set a UITableViewController to be displayed in a popover on iPad :

When I click on a row, I display an alert to warn the user of a potential destructive action. I used the new UIAlertController, and here is what happens:

The popover becomes very small (the size of the alertController view in fact). If I press Cancel, I can see the result :

Here is my code:

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

Did I miss something ?

Thanks for your help

解决方案

Found it ! If this AlertController is presented inside a popover, it must provide the location information, either a sourceView and sourceRect, or a barButtonItem.

Like

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

My code had to look like that:

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

Now the image looks like this:

这篇关于在iOS8中的Popover中呈现UIAlertController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆