Swift - How do I place a page-view within a UIAlertController? - ios

I am using Swift 3, Xcode 8.2.
I have an application where the user launches the iPhone camera and I give them a popup with instructions on how to take a good picture. I want there to be a way to create pages within the UIAlertController. I did a quick sketch of what I want to achieve.
Code wise, I am not sure what to do:
func displayInstructions() {
let insController = UIAlertController(title: "Instructions", message: "Step 1: Do this.", preferredStyle: .alert)
let actionDone = UIAlertAction(title: "OK", style: .cancel) { (action:UIAlertAction) in
//This is called when the user presses the cancel button.
print("You've pressed the done button");
}
//Add the buttons
errorController.addAction(actionDone)
// Some way to addSubviews here??
let pageViewController: UIPageViewController
insController.addSubview(pageViewController)
//Present the instruction controller
self.present(insController, animated: true, completion: nil)
}
Any help would be greatly appreciated. Thank you!

There is a property on UIAlertController, not advertised in public API but that seem usable without trouble going through the app store review. So, using KVC, you can set the contentViewController of the alert controller.
let pageViewController: UIPageViewController
// configure pageViewController...
insController.setValue(pageViewController, forKey: "contentViewController")
You can also set the size of the contentViewController by setting preferredContentSize.height on it
pageViewController.preferredContentSize.height = 180
This is what the result looks like with an empty page view controller

Related

Display an alert using storyboard and custom UITabBarController

I am dealing with a problem using UITabBarController. I have a small project using storyboards (XCode 13, IOS 15 as base system). I created a TabBarController but I later discovered I could not manage it effectively programmatically. Reading various docs, I discovered I could use two scenes from my storyboard and creating the tabbar programmatically.
So I did this in SceneDelegate.swift:
let queryViewControllerTab = storyBoard.instantiateViewController(withIdentifier: "QueryViewController")
let settingsViewControllerTab = storyBoard.instantiateViewController(withIdentifier: "SettingsViewController")
let starredViewControllerTab = storyBoard.instantiateViewController(withIdentifier: "StarredViewController")
starredViewControllerTab.tabBarItem.title = "Starred"
starredViewControllerTab.tabBarItem.image = UIImage(systemName: "star")
// TODO: Discover why first two views keep reading image I setup previously in storyboard
let tabBarController = UITabBarController()
tabBarController.viewControllers = [queryViewControllerTab, settingsViewControllerTab, starredViewControllerTab]
tabBarController.selectedViewController = settingsViewControllerTab
self.window?.rootViewController = tabBarController
self.window?.makeKeyAndVisible()
This works perfectly and I can easily put a condition whether userDefaults are not set, load directly the settings.
In my class SettingsViewController I want to add an action where, upon pressing the button, you get an alert:
#IBAction func saveButtonPressed(_ sender: UIButton) {
// keychain.set(tokenInput.text ?? "", forKey: keychainKey)
let alert = UIAlertController(title: "My Alert", message: "This is an alert.", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: NSLocalizedString("OK", comment: "Default action"),
style: .default, handler: { _ in
NSLog("The \"OK\" alert occured.")
}))
tabBarController.present(alert, animated: true, completion: nil)
}
But this makes the app crashing with unrecognized selector sent to instance 0x7f82f9705c30'
I've tried to debug the problem, and I understood I can't make the alert in this way because the view is really the tabBar and not the my scene. But here I got stuck.
I tried to implement the UITabBarControllerDelegate, in StarredViewController, but I can't get it working.
extension StarredViewController: UITabBarControllerDelegate {
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
print("did select tab bar item!")
}
}
I start thinking my main setup with SceneDelegate and AppDelegate is wrong.
Most of previous tutorials or threads I've found seems to fail even to compile because using deprecated versions.
This is a way to present an alert from any presented View Controller.
Add some extensions:
import UIKit
extension UIViewController {
var customVisibleViewController: UIViewController? {
if let navigationController = self as? UINavigationController {
return navigationController.topViewController?.customVisibleViewController
} else if let tabBarController = self as? UITabBarController {
return tabBarController.selectedViewController?.customVisibleViewController
} else if let presentedViewController = presentedViewController {
return presentedViewController.customVisibleViewController
} else if self is UIAlertController {
return nil
} else {
return self
}
}
}
extension UIApplication {
/// The top most view controller
static var topMostViewController: UIViewController? {
return UIApplication.shared.keyWindow?.rootViewController?.customVisibleViewController
}
}
Now you can show your alert in this way:
let alert = UIAlertController(title: "My Alert", message: "This is an alert.", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: NSLocalizedString("OK", comment: "Default action"),
style: .default, handler: { _ in
NSLog("The \"OK\" alert occured.")
}))
UIApplication.topMostViewController?.present(alert, animated: true, completion: nil)
This is the code to trigger an alert. With addAction, you can add possible answers.
do {
try //some method call or something else
} catch {
let alert = UIAlertController(title: "There was an error while saving!", message: "Please try again", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "I understand", style: .cancel, handler: nil))
}
you can find more information here:
how to show an alert
To get to the root Controller you can use the following code:
let viewController = UIApplication.shared.windows.first!.rootViewController as! YourViewController
I solved the problem. Actually, all my assumptions and the question were wrong.
TL;DR the storyboard is corrupted or got damaged when I removed the tab bar from it to make it programmatically.
Here the long version. Before entering in this trouble, I had a storyboard with two views and a tab bar controller. It was working perfectly. At one point, I decided I wanted to make a choice during the app starting and, in case of missing defaults, load immediately the settings view. I found that, to do this, I had to move my tab bar down to the scene delegate and remove it from storyboards. I did it, so storyboard was showing to views no linked, and I instantiated the tab bar from the scene delegate.
Weirdly, the tab bar being rendered was still showing some properties previously set on the storyboard, even if that component was deleted.
Then, you know the problem. My reasoning did not make any sense. The UITabBarController can't show any alert. Alerts can be presented on a UIViewController only. So, it was pointless to keep trying to make an alert out from a tab bar.
This wrong understanding led me also to wrong research which reported various similar questions (probably misleading).
I finally made a counter test. Created a brand new project with storyboard. Created two views on the storyboard and defined a tab bar controller on the scene delegate. It worked as expected. I linked each view to a specific UIViewController. Created a button on the view, added the IBAction and it worked. Then, I created the alert in the IBAction and, this time, worked exactly.
I ended with the same code, and the only different is that I did not create and removed a tab bar from the storyboard.
I knew that storyboard can get damaged and, probably, I did it.

How to present action sheet form a View Controller that present modally?

I have a ViewControllerA that already show as pop out,which is present modally. Inside this ViewControllerA have a tableview with tableViewCell .So in each cell have a button.When users click on this button,I want to show actionSheet at the bottom of the screen.
Here is my code:
TableViewCell class,here I connect the button to the IBAction
protocol MyDelegate: class {
func showDropDownMenu()
}
class tableCell: UITableViewCell {
weak var delegate: MyDelegate?
#IBAction func dropDownButton(_ sender: Any) {
print("print something")
self.delegate?.showDropDownMenu()
}
}
ViewControllerA
class ViewControllerA: UIViewController , UITableViewDataSource, UITableViewDelegate,MyDelegate {
func showDropDownMenu() {
let actionSheet = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
// Create your actions - take a look at different style attributes
let hideAction = UIAlertAction(title: "Hide", style: .default) { (action) in
print("didPress hide")
}
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (action) in
print("didPress cancel")
}
actionSheet.addAction(hideAction)
actionSheet.addAction(cancelAction)
self.present(actionSheet, animated: true, completion: nil)
}
Suppose when click on the button will call the function showDropDownMenu() in ViewControllerA.Now I click on dropDownButton it will show print something in console(means no problem with the button),but the actionSheet not show up on the bottom.
I not sure what is the problem here,but I suspect is cause by ViewControllerA is present using segue with properties like so:
Kind: Present modally ,Presentation: Over Current Context ,Transition:
Cover Vertical
If this is the reason,please help me how to present an actionsheet from a View Controller that presented modally. Thank you
Code for showing ViewControllerA :
func showViewControllerA(Id: Int) {
self.performSegue(withIdentifier: "showViewControllerA", sender: Id)
}
Refer this link. Though it's for IPad, it will give you a brief idea of where and how to present the action sheet
https://medium.com/#nickmeehan/actionsheet-popover-on-ipad-in-swift-5768dfa82094
I have faced a similar scenario of what you are trying to achive, and this solution which i provided above helped me out. Hope, it helps you out as Well.
Make sure you have set value for delegate. For example
cell.delegate = self;

Trouble with UIAlerts

I have been having some trouble making UIAlerts work. I have looked at a couple SO questions that seem to solve this issue yet I still have a problem. The alert view seems to not be presented. Here is my code:
var inputTextField: UITextField?
let actionSheetController: UIAlertController = UIAlertController(title: "Create a password", message: "", preferredStyle: .Alert)
let save: UIAlertAction = UIAlertAction(title: "Save", style: .Default) { action -> Void in
if !(inputTextField?.text=="password"){
println(inputTextField?.text)
}else{
println("You have a really bad password")
}
}
actionSheetController.addAction(save)
actionSheetController.addTextFieldWithConfigurationHandler { textField -> Void in
inputTextField = textField
}
self.presentViewController(actionSheetController, animated: true, completion: nil)
Here is the error:
Attempt to present <UIAlertController: 0x7fa7016305e0> on <PassProtect.ViewController: 0x7fa701576600> whose view is not in the window hierarchy!
Does anybody know why this is not being presented?
Any help or advice is greatly appreciated!
It's to do with the fact that when you make the call to present the UIAlertController, self.view is not on screen. If you are writing this code in the viewDidLoad() section, this won't work as self.view is still off screen.
You can make this call once self.view is available, for example in viewDidAppear() or from any sort of UI action like clicking a button. Basically, anything that will occur before viewDidDisappear().
There is a similar question with similar information if you want to read it and also here for a more generalised case, i.e. trying to present any sort of view controller which isn't in the view hierarchy.

How to create modal list in Swift/iOS

I'm trying to recreate a modal list popover in Swift, similar to the ones found in other several popular applications. See below for examples.
My current attempt looks like this:
#IBAction func showListOptions(sender: AnyObject) {
// segue set to "Present Modally"
// Presentation set to "Over Current Context"
self.performSegueWithIdentifier("ShowListItems", sender: self)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
var vc = segue.destinationViewController as! UIViewController
vc.view.backgroundColor = UIColor(red:0, green:0, blue:0, alpha:0.3)
}
The next view is a regular ViewController with a few buttons with their layout relative to the bottom of the screen.
A few things to note, when I set the Presentation style of the segue to "Full Screen", the background turns full black (instead of the desired alpha (rgba(0, 0, 0, 0.3)).
Setting the Presentation to "Over Current Context", I at least get the alpha color background, but I'm still able to click around on my tabbar at the bottom... which then turns my modal back to a black screen.
Help is much appreciated! Thank you.
In the second example is an UIActionSheet (easier to implement).
The first one is an UIView with low alpha (about a BlackColor with 0.5 alpha) with an UITableView constrained to bottom, trailing and leading. That with a keyframe animation. Using a UIViewController isn't the easiest approx. on this situation.
If you are targeting iOS 7.0 and up then you can do UIActionSheet
let myActionSheet = UIActionSheet()
myActionSheet.delegate = self
myActionSheet.addButtonWithTitle("Add event")
myActionSheet.addButtonWithTitle("close")
myActionSheet.cancelButtonIndex = 1
myActionSheet.showInView(self.view)
func actionSheet(myActionSheet: UIActionSheet!, clickedButtonAtIndex buttonIndex: Int){
if(myActionSheet.tag == 1){
if (buttonIndex == 0){
println("Do something")
}
}
}
if you are targeting iOS 8.0 and up then do UIAlertController
let myAlertController = UIAlertController(title: nil, message: nil, preferredStyle: UIAlertControllerStyle.ActionSheet)
let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel, handler: nil)
let otherAction = UIAlertAction(title: "other", style: .Default, handler: otherHandler)
myAlertController.addAction(cancelAction)
myAlertController.addAction(otherAction)
self.presentViewController(myAlertController, animated: true, completion: nil)
func otherHandler(alertAction: UIAlertAction) {
//Do something when other button is tapped
}
Thanks to #pbush25 - what I am really looking for is a UIAlertController of type ActionSheet. This ultimately does all the work for you :)
The tutorial (written in Swift for iOS 8) can be found here http://ioscreator.com/tutorials/action-sheet-tutorial-ios8-swift

Swift, iOS: How to use UIButton to trigger UIAlertController

I want to use an UIButton to trigger a UIAlertController... in Swift
So in this example I have an "Agree" button below some text, and I want the user to click Agree and have a pop-up alert with 2 options confirming/canceling the agreement. How would I go about connecting the UIButton and the UIAlertController. Also if the user cancels, I want the alert to dismiss and remain on the current VC. If the user agrees, I want it to segue to another view controller.
I fairly new to Swift, so if the answer could be detailed that would be extremely appreciated!
You need to add an IBAction (Control drag from your UIButton on your XIB/Storyboard, to the viewController implementation to link the button to the method). Inside the method that you link to the action you need to present the viewController, similar to the below:
func showAlert(sender: UIButton!)
{
var alert = UIAlertController(title: "Title", message: "Some Message", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Agree", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
}

Resources