Reputation: 2210
I'm attempting to make an alert the input view for a textfield, since i have max. 3 choices to make and my customer wants to have it look this way.
Basically no problem, i can tap on the textfield, resulting in showing the alert. After choosing the value in the textfield gets updated correctly, but then, the cursor stays in the textfield (im assuming because of the tap, the textfield is first responder)
The problem now is, when i tap on that very same textfield again while the cursor is in it, the default keyboard opens. So what can i do to stop it from doing this?
I have tried the following things on various locations in code:
resignFirstResponder()
becomeFirstResponder()
Also, when im currently editing another textfield with the keyboard, im able to show the alert when tapping the corresponding textfield. this results in the keyboard covering the alert, since im also not able to close the keyboard when this occurs.
Attempt against this: (Written in Event methods of other textfields
if (alertView != nil) {
alertView?.dismiss()
} //Not very professional, but still in development :)
Here are some important code snippets to understand how i built it up:
This function is the EditingDidBegin Event of the textfield
@IBAction func TextboxUnitEditing(sender: UITextField) {
//One try of preventing the cursor (not working)
tableView.endEditing(true)
...
/* Unrelated Code here */
...
alertView = SwiftAlertView(...)
...
/* Unrelated Code here */
...
alertView.show()
}
Delegate method of the alert
func alertView(alertView: SwiftAlertView, clickedButtonAtIndex buttonIndex: Int) {
//buttonIndex == 1 means, cancel was tapped
if (buttonIndex == -1) {
currentCell = nil
tableView.endEditing(true)
return
}
...
/* Change value in textfield */
}
So my question is, what goes wrong? What do i need to call to get rid of the cursor? If there is anything more you need to know, please tell me and i can provide more information.
Thanks in advance.
EDIT:
Solved. Instead of:
self.tableView.endEditing(true)
i had to do this:
dispatch_async(dispatch_get_main_queue(),{
self.tableView.endEditing(true)
})
Upvotes: 0
Views: 363
Reputation: 2210
Instead of
self.tableView.endEditing(true)
It has to be done on the main thread
dispatch_async(dispatch_get_main_queue(),{
self.tableView.endEditing(true)
})
Otherwise this line will be skipped
Upvotes: 1
Reputation: 8995
You can use this extension to do the job you seem to be describing, no text boxes needed? You call it with the line.
showInfo(message: error!.localizedDescription)
Here is the extension behind it.
protocol ShowsInfo {}
extension ShowsInfo where Self: UIViewController {
func showInfo(title: String = "Info", message: String) {
let alertController = UIAlertController(title: title, message: message, preferredStyle: .Alert)
alertController.addAction(UIAlertAction(title: "Ok", style: .Default, handler: nil))
dispatch_async(dispatch_get_main_queue()) {
self.presentViewController(alertController, animated: true, completion: nil)
}
}
}
Upvotes: 1