How to Create Custom PopUpView in ios8 & ios7? - iphone

I try to create subview inside a view but view is get full screen present view
so Can any one guide me through that Whether its possible to Customised Subview As PopupView?

You can do it by AlertViewController.
Here is my Forgot Password method, you can get help from that.
#IBAction func forgotpwd(sender: AnyObject) {
let alertController = UIAlertController(title: "Forgot Password", message: "Enter Your Email.", preferredStyle: .Alert)
alertController.addTextFieldWithConfigurationHandler { (textField) in
textField.placeholder = "Enter your Email"
textField.keyboardType = .EmailAddress
}
var yesAction = UIAlertAction(title: "YES", style: UIAlertActionStyle.Default) {
UIAlertAction in
//Do you Success button Stuff here
}
var noAction = UIAlertAction(title: "NO", style: UIAlertActionStyle.Cancel) {
UIAlertAction in
//Do you No button Stuff here, like dismissAlertView
}
alertController.addAction(yesAction)
alertController.addAction(noAction)
self.presentViewController(alertController, animated: true) {
}
}
May It help you,
HTH, Enjoy Coding!!

Related

Swift4 - Multiple Alerts with Text Field - Completion Handler

i am working at my startscreen (viewDidAppear). At the beginning of the app, there should be an alert with some notice message. This works fine. After you click "ok" at the notice, the next alert should pop up with a text field. In this text field you have to type in a double value. I need this double value to set up a lot of things inside the app, so I need this value outside the function to calculate with it.
E.g. The notice message describes how the app works. You click OK. Now the next alert pop up with the text field. For example it asks your height. After you type in your height and click OK the height-value is the main part of different calculations.
The problem is now, that my app works with the nil-value before I typed in the double value (e.g. height)
These are the snippets from the code.
var iNeedTheDataHere:String?
///Alerts
//Start
let startController = UIAlertController(title: "Notice", message: "notice message ", preferredStyle: .alert)
let inputController = UIAlertController(title: "Set input", message: "Please set the input", preferredStyle: .alert)
//BeforeScreenLoad
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
// Start Alert
startController.addAction(UIAlertAction(title: "OK", style: .default, handler:{(action:UIAlertAction!) in
self.input()
}))
self.present(startController, animated: true)
}
func input() {
inputController.addTextField()
let submitAction = UIAlertAction(title: "Submit", style: .default) { [unowned ac] _ in
let answer = inputController.textFields![0].text
iNeedTheDataHere = answer
}
inputController.addAction(submitAction)
}
When I set Breakpoints, I see that the value of the inputController is set to nil, before I typed in anything. I guess my mistake has something to do with the handler
Sorry for my English
Hope somebody can help me
let alert = UIAlertController(title: "Alert!!!", message: "Please enter your message ", preferredStyle: .alert)
alert.addTextField(configurationHandler: self.configurationTextField)
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler:{ (UIAlertAction) in
self.number.text?=""
MBProgressHUD.hide(for: self.view, animated: true)
}))
alert.addAction(UIAlertAction(title: "Done", style: .default, handler:{ (UIAlertAction) in
}))
self.present(alert, animated: true, completion: {
})
I have made some changes in your code to achieve that:
class ViewController: UIViewController {
let startController = UIAlertController(title: "Notice", message: "notice message ", preferredStyle: .alert)
let inputController = UIAlertController(title: "Set input", message: "Please set the input", preferredStyle: .alert)
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func viewDidAppear(_ animated: Bool) {
startController.addAction(UIAlertAction(title: "OK", style: .default, handler:{(action:UIAlertAction!) in
self.input()
}))
self.present(startController, animated: true)
}
func input() {
//Present another alert with textField
let confirmAction = UIAlertAction(title: "Submit", style: .default) { (_) in
guard let textFields = self.inputController.textFields,
textFields.count > 0 else {
// Could not find textfield
return
}
//get your textField
let answerTF = textFields[0]
let answer = answerTF.text
//Get values entered by user
print(answer)
}
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (_) in }
//add your textField here
inputController.addTextField { (textField) in
//Set a placeholde for textField
textField.placeholder = "Answer"
}
inputController.addAction(confirmAction)
inputController.addAction(cancelAction)
self.present(inputController, animated: true, completion: nil)
}
}
I have added comment to explain what I am doing.
And your result will be:
You can also check demo project here.

I need to create like a pop up menu where I can change data in a collection view when the collection view cell is pressed.

Basically, the question says it all. I thought about an alert but i need to actually have a text box inside and be able to save the text to a label. Any ideas on what I should use in this? I don't want to create a new view controller into a navigation. I just simply want a pop up view to open and close as needed.
All suggestions are greatly appreciated. Thanks!
You can add a textfield in your alertController.
Please refer to this answer https://stackoverflow.com/a/33000328/1754559
let alertController = UIAlertController(title: "Your alert", message: "", preferredStyle: .alert)
let saveAction = UIAlertAction(title: "Save", style: .default, handler: {
alert -> Void in
guard let textField = alertController.textFields?[0] as UITextField else { return }
yourLabel.text = textField.text
})
let cancelAction = UIAlertAction(title: "Cancel", style: .default, handler: {
(action : UIAlertAction!) -> Void in })
alertController.addTextField { (textField : UITextField!) -> Void in
textField.placeholder = "Whatever"
//Other textField configurations
}
alertController.addAction(saveAction)
alertController.addAction(cancelAction)
present(alertController, animated: true, completion: nil)

my segue is not working

I have a button that if user click on it it must show another vc and it's working properly until I add UIAlertController which it's getting data from user and after user click done on alertaction nothing happens
this is my code:
#IBAction func DfsClicked(_ sender: AnyObject) {
let alertController = UIAlertController(title: "DFS?", message: "Please input DFS depth:", preferredStyle: .alert)
let confirmAction = UIAlertAction(title: "Confirm", style: .default) { (_) in
if let field = alertController.textFields?[0] {
// store your data
self.depth = Int(field.text!)!
print(self.depth)
Puzzle.AnswerNode = dfs(inputdepth: self.depth,SortedPuzzle:self.SortedPuzzle)
} else {
// user did not fill field
}
}
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (_) in }
alertController.addTextField { (textField) in
textField.placeholder = "Default is 2"
textField.keyboardType = UIKeyboardType.numberPad
}
alertController.addAction(confirmAction)
alertController.addAction(cancelAction)
self.present(alertController, animated: true)
}
Use this method to perform segue, inside confirm action block
self.performSegue(withIdentifier: <Identifier>, sender: <Any?>)

how to display alert controller only once when the UITextfield is tapped?

I'm trying to prevent user from manipulating with their data, so when they tap on UITextfield an alertController shall be displayed only once, after they press "yes" option, then they shall be able to change data. but instead of displaying alert controller once, it keeps displaying every time I tap on UITextfield; how I can solve this issue.
func textFieldDidBeginEditing(textField: UITextField) {
let alert = UIAlertController(title: "Warning !", message: "Are your sure you want to change your content? ", preferredStyle: .Alert)
let yes = UIAlertAction(title: "Yes", style: .Cancel) { (action) in
}
alert.addAction(yes)
let no = UIAlertAction(title: "No", style: .Default) { (action) in
}
alert.addAction(no)
presentViewController(alert, animated: true) {}
}
textField.becomeFirstResponder()
}
var alertWillShow = true
func textFieldDidBeginEditing(textField: UITextField) {
if alertWillShow {
alertWillShow = false
let alert = UIAlertController(title: "Warning !", message: "Are your sure you want to change your content? ", preferredStyle: .Alert)
let yes = UIAlertAction(title: "Yes", style: .Cancel) { (action) in
}
alert.addAction(yes)
let no = UIAlertAction(title: "No", style: .Default) { (action) in
}
alert.addAction(no)
presentViewController(alert, animated: true) {}
}
}
textField.becomeFirstResponder()
}

AlertView with textbox, how to gather the data from entered textbox

Would like to gather the data from a AlertView out from the textbox. What I've found in the www is this:
#IBAction func showAlertTapped(sender: AnyObject) {
//Create the AlertController
let actionSheetController: UIAlertController = UIAlertController(title: "Add User", message: "Enter username", preferredStyle: .Alert)
//Create and add the Cancel action
let cancelAction: UIAlertAction = UIAlertAction(title: "Cancel", style: .Cancel) { action -> Void in
//Do some stuff
}
actionSheetController.addAction(cancelAction)
//Create and an option action
let saveAction: UIAlertAction = UIAlertAction(title: "Save", style: .Default) { action -> Void in
//Do some other stuff
}
actionSheetController.addAction(saveAction)
//Add a text field
actionSheetController.addTextFieldWithConfigurationHandler { textField -> Void in
//TextField configuration
textField.textColor = UIColor.blueColor()
}
//Present the AlertController
self.presentViewController(actionSheetController, animated: true, completion: nil)
}
Im not aware of the functionality of the AlertController, so I don't know where to bring the entred text to a e.g. simple variable?
Looks like UIAlertController has a textFields property and since you only have one text field you should be able to access it in the following way
let textField = actionSheetController.textFields?.first as UITextField
You can access the textfields string by assigning it to a local variable in the configuration handler.
//Create a local variable
var alertTextField:UITextField?
//Create the AlertController
let actionSheetController: UIAlertController = UIAlertController(title: "Add User", message: "Enter username", preferredStyle: .Alert)
//Create and add the Cancel action
let cancelAction: UIAlertAction = UIAlertAction(title: "Cancel", style: .Cancel) { action -> Void in
//Do some stuff
}
actionSheetController.addAction(cancelAction)
//Create and add save action
let saveAction: UIAlertAction = UIAlertAction(title: "Save", style: .Default) { action -> Void in
//Unwrap your local variable and access your textfield
if let textField = alertTextField {
println("\(textField.text)")
}
}
actionSheetController.addAction(saveAction)
//Add a text field
actionSheetController.addTextFieldWithConfigurationHandler { textField -> Void in
//TextField configuration
textField.textColor = UIColor.blueColor()
//Assign your UIAlertController textField to your local variable
alertTextField = textField
}
//Present the AlertController
self.presentViewController(actionSheetController, animated: true, completion: nil)
Swift 3 version:
//Create a local variable
var alertTextField:UITextField?
//Create the AlertController
let actionSheetController: UIAlertController = UIAlertController(title: "Add User", message: "Enter username", preferredStyle: .alert)
//Create and add the Cancel action
let cancelAction: UIAlertAction = UIAlertAction(title: "Cancel", style: .cancel) { action -> Void in
//Do some stuff
}
actionSheetController.addAction(cancelAction)
//Create and add save action
let saveAction: UIAlertAction = UIAlertAction(title: "Save", style: .default) { action -> Void in
//Unwrap your local variable and access your textfield
if let textField = alertTextField {
print(textField.text!)
}
}
actionSheetController.addAction(saveAction)
//Add a text field
actionSheetController.addTextField { textField -> Void in
//TextField configuration
textField.textColor = UIColor.blue
//Assign your UIAlertController textField to your local variable
alertTextField = textField
}
//Present the AlertController
self.present(actionSheetController, animated: true, completion: nil)