I have an alert view that I am trying to present on a photo view.
The photos are displayed in a list and can be pushed to a full-screen view.
The photo view is being displayed programmatically. I think that is what is causing the issue because the alert view is trying to present another view, on top of the (photo) view that's already presented.
The alert view is trying to display, but getting this error:
Warning: Attempt to present <UIAlertController: 0x147d2c6b0> on <LiveDeadApp.ListViewController: 0x147d614c0> which is already presenting (null)
The line that might be in question is this one:
self.present(textPrompt, animated: true, completion: nil)
This is the main list view
This is the main list view when a screenshot is taken
This is the main photo view
This is the popover in the main photo view (accessed via the "i" button)
When a screenshot is taken on the main photo view, no alert view occurs. However, when the device's orientation is changed, the photo view goes back to the list and shows the alert.
This is what I am trying to do:
Swift 3 in iOS 10
Thank you!
Here is the list view and photo view's code:
import UIKit
import Kingfisher
import SKPhotoBrowser
class ListViewCell: UITableViewCell {
@IBOutlet weak var Cellimage: UIImageView!
@IBOutlet weak var cellVenue: UILabel!
@IBOutlet weak var cellLocation: UILabel!
@IBOutlet weak var cellDate: UILabel!
@IBOutlet weak var aiView: UIActivityIndicatorView!
}
class ListViewController: UITableViewController {
var subcategory:Subcategory!
var objects:[[String:String]] = [[String:String]]()
var images = [SKPhotoProtocol]()
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
}
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.separatorStyle = .none
self.view.backgroundColor = UIColor.black
self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
navigationController!.navigationBar.barTintColor = UIColor.black
let requireTextInput = "require text input"
// add observer for screen shot
NotificationCenter.default.addObserver(forName: NSNotification.Name.UIApplicationUserDidTakeScreenshot, object: nil, queue: OperationQueue.main, using:
{ notification in
self.definesPresentationContext = true
var inputTextField = UITextField()
let textPrompt = UIAlertController(title: "Test!", message: "Testing!", preferredStyle: .alert)
textPrompt.addAction(UIAlertAction(title: "Continue", style: .default, handler: {
(action) -> Void in
// if the input match the required text
let str = inputTextField.text
if str == requireTextInput {
print("right")
} else {
print("wrong")
}
}))
textPrompt.addTextField(configurationHandler: {(textField: UITextField!) in
textField.placeholder = ""
inputTextField = textField
})
self.present(textPrompt, animated: true, completion: nil)
})
if subcategory != nil {
self.title = subcategory.title
self.objects = subcategory.photos
createLocalPhotos()
self.tableView.reloadData()
}
}
func createLocalPhotos() {
for item in objects {
let photo = SKPhoto.photoWithImageURL(item["url"]!)
photo.shouldCachePhotoURLImage = true
images.append(photo)
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return objects.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: ListViewCell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! ListViewCell
let item = objects[indexPath.row]
let title = item["title"]
let location = item["location"]
let date = item["date"]
let urlSrt = item["url"]
cell.cellVenue.text = title
cell.cellLocation.text = location
cell.cellDate.text = date
if let url = URL(string: urlSrt!) {
cell.aiView.startAnimating()
cell.Cellimage.kf.setImage(with: url, placeholder: nil, options: nil, progressBlock: nil, completionHandler: { (image, error, cacheType, url) in
cell.aiView.stopAnimating()
})
}
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath) as! ListViewCell
if(cell.Cellimage.image != nil ) {
SKPhotoBrowserOptions.displayToolbar = false
SKPhotoBrowserOptions.displayCounterLabel = false
SKPhotoBrowserOptions.displayBackAndForwardButton = false
SKPhotoBrowserOptions.displayAction = false
SKPhotoBrowserOptions.displayDeleteButton = true
SKPhotoBrowserOptions.displayHorizontalScrollIndicator = false
SKPhotoBrowserOptions.displayVerticalScrollIndicator = false
SKPhotoBrowserOptions.displayStatusbar = false
SKPhotoBrowserOptions.disableVerticalSwipe = true
SKPhotoBrowserOptions.bounceAnimation = false
let browser = ExtendedSKPhotoBrowser(originImage: cell.Cellimage.image!, photos: images, animatedFromView: cell)
let btnSize = 80//24 * UIScreen.main.scale
browser.updateCloseButton(UIImage(named: "ic_close_white")!, size: CGSize(width: btnSize, height: btnSize))
browser.updateDeleteButton(UIImage(named: "ic_info_white")!, size: CGSize(width: btnSize, height: btnSize))
browser.initializePageIndex(indexPath.row)
browser.delegate = self
present(browser, animated: true, completion: {})
browser.toggleControls()
}
}
override var prefersStatusBarHidden: Bool {
get {
return true
}
}
var popOverVC:PopUpViewController!
}
extension ListViewController: SKPhotoBrowserDelegate {
func didShowPhotoAtIndex(_ index: Int) {
}
func willDismissAtPageIndex(_ index: Int) {
}
private func willShowActionSheet(photoIndex: Int) {
// do some handle if you need
}
func didDismissAtPageIndex(_ index: Int) {
}
func didDismissActionSheetWithButtonIndex(_ buttonIndex: Int, photoIndex: Int) {
// handle dismissing custom actions
}
func removePhoto(_ browser: SKPhotoBrowser, index: Int, reload: (() -> Void)) {
popOverVC = self.storyboard?.instantiateViewController(withIdentifier: "sbPopUpID") as! PopUpViewController
popOverVC.photoData = objects[index]
}
func viewForPhoto(_ browser: SKPhotoBrowser, index: Int) -> UIView? {
return tableView.cellForRow(at: IndexPath(row: index, section: 0))
}
}
open class ExtendedSKPhotoBrowser: SKPhotoBrowser {
open override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent // white statusbar, .default is black
}
open override var prefersStatusBarHidden: Bool {
return true
}
}