在iOS8中从Popover中显示UIAlertController
我将UITableViewController设置为在iPad上的popover中显示: 当我单击一行时,我会显示一个警告,警告用户可能发生的破坏性操作。 我使用了新的UIAlertController,下面是发生的情况: popover变得非常小(实际上是alertController视图的大小)。如果我按“取消”,我可以看到结果: 这是我的密码:在iOS8中从Popover中显示UIAlertController,ios8,uipopover,uialertcontroller,Ios8,Uipopover,Uialertcontroller,我将UITableViewController设置为在iPad上的popover中显示: 当我单击一行时,我会显示一个警告,警告用户可能发生的破坏性操作。 我使用了新的UIAlertController,下面是发生的情况: popover变得非常小(实际上是alertController视图的大小)。如果我按“取消”,我可以看到结果: 这是我的密码: override func tableView(tableView: UITableView, didSelectRowAtIndexPat
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)
}
}
我错过什么了吗
感谢您的帮助我也遇到了同样的问题,无法解决如何防止popover重新调整大小的问题。使用警报而不是操作表也会导致弹出框调整大小。我发现的解决方法是通过将模态表示样式设置为
UIModalPresentationPopover
,将动作表本身用作popover。我知道你在用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中将其作为一个实用方法,这样我就可以从我所有的弹出窗口中调用它。这是可行的,但并不是一个理想的解决方案,因为如果有什么东西在你的一个弹出窗口打开时引发警报,它可能会被调整大小。请让我知道,如果你想知道如何防止发生任何调整大小。祝你好运 找到了!
如果此AlertController显示在popover中,它必须提供位置信息,可以是sourceView和sourceRect,也可以是barButtonItem。
像
我的代码必须是这样的:
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)
}
}
现在,图像如下所示:
这里也有同样的问题:-(
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)
}
}