logo
down
shadow

UIAlertController get text


UIAlertController get text

By : Renuka Priyadarshani
Date : November 17 2020, 01:00 AM
wish helps you I am trying to get the text from the UIAlertController textfield. Can someone tell me what I did wrong as its not working. I get a NIL return. , Here's the 'clean copy/paste' version:
Swift 3 & 4:
code :
let alert = UIAlertController(title: "Alert Title",
                              message: "Alert message",
                              preferredStyle: UIAlertControllerStyle.alert)

let ok = UIAlertAction(title: "OK",
                       style: UIAlertActionStyle.default) { (action: UIAlertAction) in

                        if let alertTextField = alert.textFields?.first, alertTextField.text != nil {

                            print("And the text is... \(alertTextField.text!)!")

                        }


}

let cancel = UIAlertAction(title: "Cancel",
                           style: UIAlertActionStyle.cancel,
                           handler: nil)

alert.addTextField { (textField: UITextField) in

    textField.placeholder = "Text here"

}

alert.addAction(ok)
alert.addAction(cancel)

self.present(alert, animated: true, completion: nil)
let alert = UIAlertController(title: "Alert Title",
                              message: "Alert message",
                              preferredStyle: UIAlertControllerStyle.Alert)

let ok = UIAlertAction(title: "OK",
                       style: UIAlertActionStyle.Default) { (action: UIAlertAction) in

                        if let alertTextField = alert.textFields?.first where alertTextField.text != nil {

                            print("And the text is... \(alertTextField.text!)!")

                        }


}

let cancel = UIAlertAction(title: "Cancel",
                           style: UIAlertActionStyle.Cancel,
                           handler: nil)

alert.addTextFieldWithConfigurationHandler { (textField: UITextField) in

    textField.placeholder = "Text here"

}

alert.addAction(ok)
alert.addAction(cancel)

self.presentViewController(alert, animated: true, completion: nil)
UIAlertController *alert = [UIAlertController
                           alertControllerWithTitle: @"Alert Title"
                           message: @"Alert message"
                           preferredStyle:UIAlertControllerStyleAlert];

UIAlertAction *ok = [UIAlertAction actionWithTitle: @"OK" style: UIAlertActionStyleDefault
                                           handler:^(UIAlertAction *action){


                                               UITextField *alertTextField = alert.textFields.firstObject;

                                               NSLog(@"And the text is... %@!", alertTextField.text);

                                           }];

UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel
                                               handler: nil];


[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {

    textField.placeholder = @"Text here";

}];

[alert addAction:ok];
[alert addAction:cancel];

[self presentViewController:alert animated:YES completion:nil];
UIAlertController *alertController = ...// Create alert

// Assuming you called 'addTextFieldWithConfigurationHandler' on 'alertController'

UIAlertAction *action = [UIAlertAction actionWithTitle: ... handler:^(UIAlertAction * action) {

    // alertController.textFields should hold the alert's text fields.
}
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
            textField.placeholder = @"Enter the name of the recipe";
            textField.keyboardType = UIKeyboardTypeDefault;

            myTf = textField;
        }];
UIAlertAction* ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
                                               handler:^(UIAlertAction * action){...
//UITextField *temp = alert.textFields.firstObject;

// Get the text from your textField
NSString *temp = myTf.text;


Share : facebook icon twitter icon
Swift UIAlertController Getting Text Field Text

Swift UIAlertController Getting Text Field Text


By : Jeppe Friberg
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , The UIAlertController has a textFields property. That's its text fields. Any of your handlers can examine it and thus can get the text from any of the text fields.
UIAlertController : UICollectionViewFlowLayout is not defined warning every time I try to bringup a UIAlertcontroller

UIAlertController : UICollectionViewFlowLayout is not defined warning every time I try to bringup a UIAlertcontroller


By : The Gick
Date : October 29 2020, 04:46 AM
I wish this helpful for you I am using a UIAlertController for taking user input and updating a table cell. Everytime when I try to create a alert, I get this following warning in console , You need to call
code :
alertcontroller.view.setNeedsLayout()
self.presentViewController(alertController, animated: true, completion: nil)
UIAlertController Giving error? Please Give the code for UIAlertController with Array of Strings As Buttons in Swift

UIAlertController Giving error? Please Give the code for UIAlertController with Array of Strings As Buttons in Swift


By : newby
Date : March 29 2020, 07:55 AM
this one helps. First of all I going to explain you why this happen, you can see if you touch any of your buttons always the output result of the call of NSLog("%@",String(i)) is plLists.count - 1, this is because you're trying to set always the index of the for statement inside the closure of the actions you're adding. The variable is outside the scope of the closure.
The main problem is that always the reference retained inside the closure in your case is the last index of the array, because as you said is increased and finally when it finish is assigned the value.
code :
func presentAction() {

    let alertA = UIAlertController(title: "PlayLists", message: "Select From PlayLists Below", preferredStyle: .ActionSheet)
    let action = UIAlertAction(title: "Cancel", style:  .Default, handler: nil)

    let plLists = ["List1", "List2", "List3", "List4"]
    for(var i = 0; i < plLists.count; i++) {
        alertA.addAction(UIAlertAction(title: plLists[i], style: .Default, handler: self.handlerForAction)) 
    }

    alertA.addAction(action)
    self.presentViewController(alertA, animated: true, completion: nil)
}

func handlerForAction(action: UIAlertAction) {
    print(action.title!)
}
Select text in UIAlertController's text field

Select text in UIAlertController's text field


By : ankush agrawal
Date : March 29 2020, 07:55 AM
this will help I have rewrote your code. Your class should conform to the UITextFieldDelegate protocol and implement the textFieldDidBeginEditing method, like this:
code :
class ViewController: UIViewController, UITextFieldDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()

        let ac = UIAlertController(title: "Rename", message: nil, preferredStyle: .Alert)
        ac.addTextFieldWithConfigurationHandler({
            [] (textField: UITextField) in
            textField.text = "filename.dat"
            textField.delegate = self

        })
        ac.addAction(UIAlertAction(title: "CANCEL", style: .Cancel, handler: nil))
        ac.addAction(UIAlertAction(title: "OK", style: .Default, handler: {
            [] Void in
            // do something
        }))
        dispatch_async(dispatch_get_main_queue(), {
            self.presentViewController(ac, animated: true, completion: nil)
        })

    }
    func textFieldDidBeginEditing(textField: UITextField) {
        textField.selectedTextRange = textField.textRangeFromPosition(textField.beginningOfDocument, toPosition: textField.endOfDocument)
        textField.becomeFirstResponder()
    }

}
How to get access to button placed on UIAlertController from handler of UITextField placed on UIAlertController?

How to get access to button placed on UIAlertController from handler of UITextField placed on UIAlertController?


By : Aarti Jadhav
Date : March 29 2020, 07:55 AM
wish of those help Why don't you make an attribute of the UIAlertAction? I've changed your code:
code :
class TVC_ProgramsList: UITableViewController {

  var enterNameAC = UIAlertController()
  let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil)

  @IBAction func addpush(sender: UIBarButtonItem) {
     addProgram()
  }

  func addProgram() {
    enterNameAC = UIAlertController(title: "Adding program", message: "Enter program name", preferredStyle: UIAlertControllerStyle.Alert)
    enterNameAC.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: nil))
    enterNameAC.addAction(self.okAction)
    enterNameAC.addTextFieldWithConfigurationHandler { (textField) -> Void in
        textField.placeholder = "Program name"
        textField.addTarget(self, action: #selector(TVC_ProgramsList.enterProgramNameAC_editingTextField), forControlEvents: UIControlEvents.AllEditingEvents)

    }

    self.presentViewController(enterNameAC, animated: true, completion: nil)

  }

  func enterProgramNameAC_editingTextField() {
    let programNameString = enterNameAC.textFields![0].text!
    // use okAction here 
    self.okAction.enabled = programNameString.characters.count > 0
  }
}
Related Posts Related Posts :
  • Looking for simplest option to render Razor cshtml pages in a console application without any web server
  • Evaluating variables at a specific time in Modelica
  • When I run the Application, only "web" engine is running in GlassFish. "webservices" is not started
  • How To Set MIME Type Of Google Drive File
  • Remove Missing Values in Weka
  • Reloading a UICollectionView using reloadData method returns immediately before reloading data
  • carrot2 - can I cluster documents from a folder?
  • StreamSocket has no Close Implementation in C#
  • Rails, Foundation 4, Respond.js not working properly in IE8
  • How can i create imagesurface from cairo xlib's Graphics Context using cairo and x11 Api's?
  • CKEditor "overflow: scroll" on parent causes toolbar to freeze at initial position
  • Differences between components and controls in ENYO
  • Photoshop making isometric?
  • Does Intel IPP 8.0 support in-place operations?
  • What is Object dictionary in CANOpen?
  • Example of orbBasic Indexed User Variables
  • convert to ABSOLUTE in logback
  • How to conditionally download file using p:fileDownload
  • Error on pod install
  • Set HTTP GET Parameters in Finagle
  • different attack that uses sql injection
  • How can I change my xampp username not as 'root'
  • AMQP Content header payload structure
  • Apache POI formula evaluation not working for Excel IF
  • How can I trace RESTEasy's dispatch?
  • Map Freezes on iOS 7 with Google Maps SDK 1.4
  • Comparing lists, is the subset list within the first list
  • Non-ascii character highlight in Sublime Text 2
  • Installing Magit in Aquamacs
  • Receiving error - System.Net.Mail.SmtpException: 4.3.2 try again later
  • Coreaudio render callback in monotouch
  • The command 'yarn --v' also initiates 'yarn install' and installs packages automatically. Why is this happening?
  • save multiple matches in a list (grep or awk)
  • Can a number register be used in a groff request?
  • Mapping FAQ with RASA for large dataset (2000+)
  • Fragment not receiving LiveData updates after remove + add
  • FitText.js makes text bigger rather than smaller
  • ARM - Implementing stack with load/store multiple register values
  • How to check if a ChromeCast Session is already in progress
  • ngForm inside a Carousel Slide in UI Bootstrap not working
  • Clearing attributes in Tritium
  • "vagrant up" failing: Vagrant VM failed to remain in the running state
  • ftsearch returning empty docs
  • What are the advantages of setting "hive.exec.parallel" to false in Hive ?
  • Creating a root certificate in FiddlerCore
  • How to access app.config in a blueprint?
  • DB2 RECORDSET table name converted to uppercase
  • Resizing the superview according to the subviews
  • IExpress - Disable Compression
  • Getting InvalidProtocolBufferException while running oozie job
  • What are the differences between Play run and start?
  • How can I share props in ReasonReact?
  • Task.Delay is skipped
  • Parsley.js Password Confirm doesn‘t work
  • How to get all registred 'browser:resource' in Plone
  • Overriding page_list controller inside a package in Concrete5.6.1.2
  • Robolectric 2.x - dependent jars are downloading while running the tests
  • Setting Flyout to Main Frame Navigation(Windows 8.1 app store)
  • Build project - Nuget Error
  • How to recover admin password for SonarQube
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org