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.
Related
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)
}
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
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 :)
I am quite new to Swift development and I tried referring to the Swift UIAlertController API but couldn't figure out how to navigate to another UIViewController after clicking a button on the UIAlertController.
I would appreciate any pointers, help or a solution to this problem. My code snippet is given below -
#IBAction func showAlert() {
let alertController = UIAlertController(title: "Disclaimer", message: "Disclaimer Text Here", preferredStyle: .Alert)
let declineAction = UIAlertAction(title: "Decline", style: .Cancel, handler: nil)
alertController.addAction(declineAction)
let acceptAction = UIAlertAction(title: "Accept", style: .Default) { (_) -> Void in
let secondVC = ViewController(nibName: "ViewController", bundle: nil)
let navController = UINavigationController(rootViewController: secondVC)
self.presentViewController(navController, animated: true, completion: nil)
}
alertController.addAction(acceptAction)
presentViewController(alertController, animated: true, completion: nil)
}
What I am trying to do here is when a button is clicked the disclaimer AlertController is displayed. If "Decline" button is selected, cancel action is performed and if "Accept" is selected the app should navigate to a Navigation Controller which then allows me to navigate to other ViewControllers using a menu in the application.
Earlier I used the story board to link a button to the NavigationController to traverse to the ViewController I desired. Now I want to do the same programmatically for the AlertController "Accept" Button.
Thank you in advance.
You need to implement the handler block to perform code when an action is selected :
#IBActionfunc showAlert() {
let alertController = UIAlertController(title: "Disclaimer", message: "Disclaimer Text Here", preferredStyle: .Alert)
let declineAction = UIAlertAction(title: "Decline", style: .Cancel, handler: nil)
alertController.addAction(declineAction)
let acceptAction = UIAlertAction(title: "Accept", style: .Default) { (_) -> Void in
let secondVC = SecondViewController(nibName: "SecondView", bundle: nil)
let navController = UINavigationController(rootViewController: secondVC)
self.presentViewController(navController, animated: true, completion: nil)
}
alertController.addAction(acceptAction)
presentViewController(alertController, animated: true, completion: nil)
}
Basically to link the UIAlertViewController button to a UINavigationController's UIViewController we would need to create a manual segue between the UIViewController which has the UIAlertViewController to the UINavigationController.
Please refer to this screen shot to see how to do the above -
Then select the link between the UIViewController and the UINavigationController. Go to the left sidebar and select the Attributes inspector and name the identifier.
Now write the following in your code -
#IBAction func showAlert() {
let alertController = UIAlertController(title: "Disclaimer", message: "Before using this teaching resource, you confirm that you agree:\n1. To obey the law regarding data protection and patient confidentiality.\n2. To us this app professionally and appropriately in clinical settings.\n3. This is for your personal use and you may not modify, distribute, publish, transfer any information obtained from this teaching resource without the developers' permission.\n4. In no event shall the developer be liable to you for any loss arising from your use of this resource.", preferredStyle: .Alert)
let declineAction = UIAlertAction(title: "Decline", style: .Cancel, handler: nil)
let acceptAction = UIAlertAction(title: "Accept", style: .Default) { (_) -> Void in
self.performSegueWithIdentifier("SomeSegue", sender: self) // Replace SomeSegue with your segue identifier (name)
}
alertController.addAction(declineAction)
alertController.addAction(acceptAction)
presentViewController(alertController, animated: true, completion: nil)
}
That's the use of the handler block. You should make your desired action in this block.
Edit: Code
let acceptAction = UIAlertAction(title: "Accept", style: .Default, handler:{ action in
//Write your code here
})
You can use this link as a reference: http://www.appcoda.com/uialertcontroller-swift-closures-enum/
I have no idea how to change screens programatically. I have an alert view and I want to be able to change screen when the user presses the "Ok" button. How do I do this?
Here is my new code:
func showAlertController(){
let tilte = "My Medication"
let message = NSLocalizedString("Go through Medication guide?", comment: "")
let cancelButtonTitle = NSLocalizedString("Dismiss", comment: "")
let otherButtonTitle = NSLocalizedString("Ok", comment: "")
let alertController = UIAlertController(title: title, message: message, preferredStyle: .Alert)
let cancelAction = UIAlertAction(title: cancelButtonTitle, style: .Cancel){ action in
NSLog("User said no")}
let otherAction = UIAlertAction(title: otherButtonTitle, style: .Default){action in
// I know I need to put something in here.
let appointment = Appointment()
self.presentViewController(appointment, animated:true, completion:nil)
}
alertController.addAction(cancelAction)
alertController.addAction(otherAction)
self.presentViewController(alertController, animated: true, completion: nil)
}
However now I get a bug saying:
Thread 1: EXC_BAD_INSTRUCTION(code=EXC_l1386_INVOP,subcode=0x0)
Add presentViewController inside UIAlertAction closure for "Ok" button, it means that the button is pressed and so you do your stuffs for the button being pressed inside the block.
class MainViewController: UIViewController {
...
...
func showAlertController(){
let tilte = "My Medication"
let message = NSLocalizedString("Go through Medication guide?", comment: "")
let cancelButtonTitle = NSLocalizedString("Dismiss", comment: "")
let otherButtonTitle = NSLocalizedString("Ok", comment: "")
let alertController = UIAlertController(title: title, message: message, preferredStyle: .Alert)
let cancelAction = UIAlertAction(title: cancelButtonTitle, style: .Cancel){ action in
NSLog("User said no")}
let otherAction = UIAlertAction(title: otherButtonTitle, style: .Default){action in
// I know I need to put something in here.
let appointmentViewController = AppointmentViewController()
self.presentViewController(appointmentViewController, animated:true, completion:nil)
}
alertController.addAction(cancelAction)
alertController.addAction(otherAction)
self.presentViewController(alertController, animated: true, completion: nil)
}
...
...
}
class AppointmentViewController: UIViewController {
}