How can I get following pop up text field in Swift? - ios

I would like to a let user create a folder in my app. More specifically, when the user presses "add folder" button, I would like to have the following text field to pop up
How can I implement it?

It's going to look something like this:
// create the actual alert controller view that will be the pop-up
let alertController = UIAlertController(title: "New Folder", message: "name this folder", preferredStyle: .alert)
alertController.addTextField { (textField) in
// configure the properties of the text field
textField.placeholder = "Name"
}
// add the buttons/actions to the view controller
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
let saveAction = UIAlertAction(title: "Save", style: .default) { _ in
// this code runs when the user hits the "save" button
let inputName = alertController.textFields![0].text
print(inputName)
}
alertController.addAction(cancelAction)
alertController.addAction(saveAction)
present(alertController, animated: true, completion: nil)
Let me know if this helps.

For custom PopUp:
By Storyboard
Step 1:
Create a UIViewController with this pop up message.
Step 2:
Intead of push, Present it from your parent View and set Parent View transition style to cross dissolve.
By Code:
if let nav = self.navigationController
{
UIView.transition(with:nav.view, duration:0.25, options:.transitionCrossDissolve, animations: {
_ = nav.popViewController(animated:false)
}, completion:nil)
}

Related

How to edit alertViewController without dismissing it accordingly to the action

I have an alert in my app where I put a textfield. The user can use it to add some values in an array. However I want all the values to be different. So if a user inserts an existing value, I want the textfield to be cleared and present a different placeholder text telling the user to insert a new value.
This is what I'm doing now:
func appendWord(){
let alertController = UIAlertController(title:"insert a word", message: nil, preferredStyle: UIAlertController.Style.alert)
alertController.addTextField { (textField : UITextField) -> Void in
textField.placeholder = "insert here"
textField.delegate = self
}
let cancelAction = UIAlertAction(title: "cancel", style: UIAlertAction.Style.cancel) { (result : UIAlertAction) -> Void in
}
let okAction = UIAlertAction(title: "save", style: UIAlertAction.Style.default) { (result : UIAlertAction) -> Void in
let newName = alertController.textFields![0].text! as String
//Useless Stuff to Append items here [...]
//If the item already exists then i call the following function which is inside of an if statement...
self.errorInCreation()
}
alertController.addAction(cancelAction)
alertController.addAction(okAction)
self.present(alertController, animated: true, completion: nil)
}
func errorInCreation(){
let alertController = UIAlertController(title:"Insert a new word", message: nil, preferredStyle: UIAlertController.Style.alert)
alertController.addTextField { (textField : UITextField) -> Void in
textField.placeholder = "The word already exists. Insert a new one"
textField.attributedPlaceholder = NSAttributedString(string: "The word already exists. Insert a new one",attributes: [NSAttributedString.Key.foregroundColor: UIColor.red])
textField.delegate = self
}
let cancelAction = UIAlertAction(title: "cancel", style: UIAlertAction.Style.cancel) { (result : UIAlertAction) -> Void in
}
let okAction = UIAlertAction(title: "save", style: UIAlertAction.Style.default) { (result : UIAlertAction) -> Void in
let newName = alertController.textFields![0].text! as String
//Useless Stuff to Append items here [...]
//If the item already exists then i call the following function which is inside of an if statement...
self.errorInCreation()
}
alertController.addAction(cancelAction)
alertController.addAction(okAction)
self.present(alertController, animated: true, completion: nil)
}
This should present a new alertViewController until the user inserts a new word. However this doesn't happen. When I press the save button, the alert closes.
I tried to edit the current alert but it's not really possible.
How could I clear the inserted text, change the placeholder name and let the user insert a new word?
I found this person who has my same problem but the solution pointed out here didn't work.
Presenting new AlertViewController after dismissing the previous AlertViewController - Swift
The solution is actually quite simple: don't use UIAlertController. It's just a specialized presented view controller, and you don't get much control over how it looks or behaves; in particular, it dismisses when a button is tapped, which is not what you want. So just use a custom presented view controller where you have the kind of control you're after.

How exactly would my alert controller action append text to a label?

Basically, i have an alert controller set up so that when i click a button in the view controller, an alert controller pops up and i can type words into a textfield, click an "OK" button.
Reference image
(source: ashfurrow.com)
and it inserts that text into a label in the view controller. Ive extended this ability so that i can have preset keywords (like "Good," "Likely" and "Almost") that i can select from in the alert controller to speed up the typing process, as these words are typed a lot in my app. which is shown in the below image
.
I was wondering if you could also add those key words that I've selected to whatever is in the label? Every time i try to click a preset keyword to add to the label, it erases whatever was already in the label.
Specifically, whatever text that is already in my label, can i select a preset keyword to append to whats in the label?
This is what i have so far, for the second image:
//Editable Text Box with Preset Keywords
#IBOutlet weak var UselessLabel: UILabel!
#IBAction func UselessTapped(_ sender: UIButton) {
print("Useless Button Tapped")
openUselessAlert()
}
func openUselessAlert() {
//Create Alert Controller
let alert9 = UIAlertController (title: "Uselss:", message: nil, preferredStyle: UIAlertControllerStyle.alert)
//preset keyword as button in alert controller
let bt1 = UIAlertAction(title: "Good", style: UIAlertActionStyle.default){
(action) in self.UselessLabel.text = "Good"}
alert9.addAction(bt1)
//preset keyword as button in alert controller
let bt2 = UIAlertAction(title: "Likely", style: UIAlertActionStyle.default){
(action) in self.UselessLabel.text = "Likely"}
alert9.addAction(bt2)
//preset keyword as button in alert controller
let bt3 = UIAlertAction(title: "Almost", style: UIAlertActionStyle.default){
(action) in self.UselessLabel.text = "Almost"}
alert9.addAction(bt3)
//Create Cancel Action
let cancel9 = UIAlertAction(title: "CANCEL", style: UIAlertActionStyle.cancel, handler: nil)
alert9.addAction(cancel9)
//Create OK Action
let ok9 = UIAlertAction(title: "OK", style: UIAlertActionStyle.default) { (action: UIAlertAction) in print("OK")
let textfield = alert9.textFields?[0]
print(textfield?.text!)
self.UselessLabel.text = textfield?.text!
}
alert9.addAction(ok9)
//Add Text Field
alert9.addTextField { (textfield: UITextField) in
textfield.text = self.UselessLabel.text
textfield.placeholder = "Useless"
}
//Present Alert Controller
self.present(alert9, animated:true, completion: nil)
}
Please help! How exactly would you do this in swift 3?
To add the action text to a UILabel you can use
let bt3 = UIAlertAction(title: "Almost", style: UIAlertActionStyle.default){
(action) in self.UselessLabel.text = self.UselessLabel.text + action.title}
and if you want to add to UITextField in the AlertView then you must do something like this
let bt3 = UIAlertAction(title: "Almost", style: UIAlertActionStyle.default){
(action) in if(alert9.textFields.count > 0)
{
alert9.textFields[0].text = alert9.textFields[0].text + action.title
}
}
I hope this helps you

How would I click a uialertcontroller action to enter/edit text into a uialertcontroller textfield? Swift 3, Xcode 8, IOS

Basically, i have an alert controller set up so that when i click a button in the view controller, an alert controller pops up and i can type words into a textfield, click an "OK" button and it inserts that text into a label in the view controller. Ive extended this ability so that i can have preset keywords (like "Good," "Likely" and "Almost") that i can select from in the alert controller to speed up the typing process, as these words are typed a lot in my app. I was wondering if you could also edit those key words that I've selected? So instead of clicking my preset word and it just being sent to my label, can i send it to my textfield, edit it, and then send it to my label with the OK button?
//Editable Text Box with Preset Keywords
#IBOutlet weak var UselessLabel: UILabel!
#IBAction func UselessTapped(_ sender: UIButton) {
print("Useless Button Tapped")
openUselessAlert()
}
func openUselessAlert() {
//Create Alert Controller
let alert9 = UIAlertController (title: "Uselss:", message: nil, preferredStyle: UIAlertControllerStyle.alert)
//preset keyword as button in alert controller
let bt1 = UIAlertAction(title: "Good", style: UIAlertActionStyle.default){
(action) in self.UselessLabel.text = "Good"}
alert9.addAction(bt1)
//preset keyword as button in alert controller
let bt2 = UIAlertAction(title: "Likely", style: UIAlertActionStyle.default){
(action) in self.UselessLabel.text = "Likely"}
alert9.addAction(bt2)
//preset keyword as button in alert controller
let bt3 = UIAlertAction(title: "Almost", style: UIAlertActionStyle.default){
(action) in self.UselessLabel.text = "Almost"}
alert9.addAction(bt3)
//Create Cancel Action
let cancel9 = UIAlertAction(title: "CANCEL", style: UIAlertActionStyle.cancel, handler: nil)
alert9.addAction(cancel9)
//Create OK Action
let ok9 = UIAlertAction(title: "OK", style: UIAlertActionStyle.default) { (action: UIAlertAction) in print("OK")
let textfield = alert9.textFields?[0]
print(textfield?.text!)
self.UselessLabel.text = textfield?.text!
}
alert9.addAction(ok9)
//Add Text Field
alert9.addTextField { (textfield: UITextField) in
textfield.placeholder = "Useless"
}
//Present Alert Controller
self.present(alert9, animated:true, completion: nil)
}
Please help! Im really new to Xcode and programming in general, so I'm a huge idiot when it comes to this.
Thanks :)

Go to previous controller in navigation stack (Swift)

I am trying to popViewController to the previous View Controller in the navigation stack after an alertView. As of now, my program will not run the popViewController method, since the alertView is in the way, and brings up the error:
UINavigationController 0x17670900 while an existing transition or presentation is occurring; the navigation stack will not be updated.
How do I go about running the popViewController method after the user clicks OK from the alertView? Do I have to set a delegate which detects when the used clicks OK?
Here is my code:
//alertView after Picture saved
let alertView = UIAlertController(title: "Success!", message: "Record Saved to Database", preferredStyle: .Alert)
alertView.addAction(UIAlertAction(title: "Ok", style: .Default, handler: nil))
self.presentViewController(alertView, animated: true, completion: nil)
//go to previous controller using popViewController, doesnt work, brings up error message
if let navController = self.navigationController {
navController.popViewControllerAnimated(true)
}
You need to pop the view controller when user presses the Ok button from AlertView.
let alertView = UIAlertController(title: "Success!", message: "Record Saved to Database", preferredStyle: .Alert)
let OKAction = UIAlertAction(title: "Ok", style: .Default) { (action) in
// pop here
if let navController = self.navigationController {
navController.popViewControllerAnimated(true)
}
}
alertView.addAction(OKAction)
self.present(alertView, animated: true, completion: nil)
You can put the "popViewControllerAction" inside an alert action like this.
func alertMethod() {
var okAlertController = UIAlertController(title: NSLocalizedString("Your title", comment: ""), message: "Your message", preferredStyle:
UIAlertControllerStyle.Alert)
let okAction = UIAlertAction(title: NSLocalizedString("Ok", comment: ""), style: UIAlertActionStyle.Default) { (UIAlertAction) -> Void in
// your action - navController.popViewControllerAnimated(true)
}
let cancelAction = UIAlertAction(title: NSLocalizedString("Cancel", comment: ""), style: UIAlertActionStyle.Default) { (UIAlertAction) -> Void in
// your action
}
}
saveAlertController.addAction(okAction)
saveAlertController.addAction(cancelAction)
self.presentViewController(okAlertController, animated: true, completion: nil)
}
That should work, in that case the method gets called after the user presses a button...

Add function to button in alertcontroller

I am new to swift and I have followed some tutorial in alert view lesson. I want to add function like call another viewcontroller whenever the alert's button is clicked but i dont know how. so pls help me
func showAlertController(){
var title : String = "hi!"
var message : String = NSLocalizedString("Are you feeling well? ", comment:"")
let cancelButtonTitle = NSLocalizedString("No", comment:"")
let otherButtonTitle = NSLocalizedString("Yes", comment:"")
let alertController = UIAlertController(title: title, message: message, preferredStyle: .Alert)
let cancelAction = UIAlertAction(title: cancelButtonTitle, style: .Cancel){
action in NSLog("No!!!!")
}
let otherAction = UIAlertAction(title: otherButtonTitle, style: .Default){
action in NSLog("welcome!!! hello back")
}
alertController.addAction(cancelAction)
alertController.addAction(otherAction)
presentViewController(alertController, animated: true, completion: nil)
}
Try this code:
let cancelAction = UIAlertAction(title: cancelButtonTitle, style: .Cancel){
action in NSLog("No!!!!")
let View2 = self.storyboard?.instantiateViewControllerWithIdentifier("View2") as TwoViewController
self.navigationController?.pushViewController(View2, animated: true)
}
and also select your existing view controller and then From the drop down menu select Edit>Embed in > Navigation Controller.
After that add a new view controller and create a new Cocoa Class and name it TwoViewController SubClass of UIViewController.
After that select your new ViewController and Customise it this way from Identity Inspector
May be this can help you.

Resources