How to enable/disable Alert action?
I want to enable the action of the alert only if the user has type in some input with the following codelines:
var alert: UIAlertController!
func alertBeitrageMelden(postId: String){
self.alert = UIAlertController(title: "Beitrag melden", message: "Wieso möchtest du den Beitrag melden?", preferredStyle: .alert)
let defaultAction = UIAlertAction(title: "Melden", style: .default) { (action) in
if let text = self.alert.textFields?.first?.text, text.count > 0 {
let postTime = Date().timeIntervalSince1970
let ref = Database.database().reference().child("gemeldeteBeitraege").child(postId)
ref.setValue(["postId": postId, "reason": text, "time": postTime])
ProgressHUD.showSuccess("Der Beitrag wurde gemeldet", interaction: false)
}
}
let cancelAction = UIAlertAction(title: "Abbrechen", style: .cancel) { (action) in
}
alert.addTextField { (textField) in
textField.placeholder = ""
textField.delegate = self
}
alert.addAction(defaultAction)
alert.addAction(cancelAction)
self.present(alert, animated: true) {
}
}
Here I check if the user typed in something:
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if (textField.text?.count)! > 0 {
self.alert.actions[0].isEnabled = true
}else{
self.alert.actions[0].isEnabled = false
}
return true;
}
But its still not working.
Thanks in advance for your help!
swift uitextfield alert
add a comment |
I want to enable the action of the alert only if the user has type in some input with the following codelines:
var alert: UIAlertController!
func alertBeitrageMelden(postId: String){
self.alert = UIAlertController(title: "Beitrag melden", message: "Wieso möchtest du den Beitrag melden?", preferredStyle: .alert)
let defaultAction = UIAlertAction(title: "Melden", style: .default) { (action) in
if let text = self.alert.textFields?.first?.text, text.count > 0 {
let postTime = Date().timeIntervalSince1970
let ref = Database.database().reference().child("gemeldeteBeitraege").child(postId)
ref.setValue(["postId": postId, "reason": text, "time": postTime])
ProgressHUD.showSuccess("Der Beitrag wurde gemeldet", interaction: false)
}
}
let cancelAction = UIAlertAction(title: "Abbrechen", style: .cancel) { (action) in
}
alert.addTextField { (textField) in
textField.placeholder = ""
textField.delegate = self
}
alert.addAction(defaultAction)
alert.addAction(cancelAction)
self.present(alert, animated: true) {
}
}
Here I check if the user typed in something:
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if (textField.text?.count)! > 0 {
self.alert.actions[0].isEnabled = true
}else{
self.alert.actions[0].isEnabled = false
}
return true;
}
But its still not working.
Thanks in advance for your help!
swift uitextfield alert
your textfield delegate is working or not ?
– Taimoor Suleman
Jan 2 at 11:52
Yes it works. this time, the action is enabled if there is one character inside the textfield but that makes no sense.
– jo1995
Jan 2 at 11:54
Repoen the xcode, clean your porject and run again
– Taimoor Suleman
Jan 2 at 11:55
add a comment |
I want to enable the action of the alert only if the user has type in some input with the following codelines:
var alert: UIAlertController!
func alertBeitrageMelden(postId: String){
self.alert = UIAlertController(title: "Beitrag melden", message: "Wieso möchtest du den Beitrag melden?", preferredStyle: .alert)
let defaultAction = UIAlertAction(title: "Melden", style: .default) { (action) in
if let text = self.alert.textFields?.first?.text, text.count > 0 {
let postTime = Date().timeIntervalSince1970
let ref = Database.database().reference().child("gemeldeteBeitraege").child(postId)
ref.setValue(["postId": postId, "reason": text, "time": postTime])
ProgressHUD.showSuccess("Der Beitrag wurde gemeldet", interaction: false)
}
}
let cancelAction = UIAlertAction(title: "Abbrechen", style: .cancel) { (action) in
}
alert.addTextField { (textField) in
textField.placeholder = ""
textField.delegate = self
}
alert.addAction(defaultAction)
alert.addAction(cancelAction)
self.present(alert, animated: true) {
}
}
Here I check if the user typed in something:
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if (textField.text?.count)! > 0 {
self.alert.actions[0].isEnabled = true
}else{
self.alert.actions[0].isEnabled = false
}
return true;
}
But its still not working.
Thanks in advance for your help!
swift uitextfield alert
I want to enable the action of the alert only if the user has type in some input with the following codelines:
var alert: UIAlertController!
func alertBeitrageMelden(postId: String){
self.alert = UIAlertController(title: "Beitrag melden", message: "Wieso möchtest du den Beitrag melden?", preferredStyle: .alert)
let defaultAction = UIAlertAction(title: "Melden", style: .default) { (action) in
if let text = self.alert.textFields?.first?.text, text.count > 0 {
let postTime = Date().timeIntervalSince1970
let ref = Database.database().reference().child("gemeldeteBeitraege").child(postId)
ref.setValue(["postId": postId, "reason": text, "time": postTime])
ProgressHUD.showSuccess("Der Beitrag wurde gemeldet", interaction: false)
}
}
let cancelAction = UIAlertAction(title: "Abbrechen", style: .cancel) { (action) in
}
alert.addTextField { (textField) in
textField.placeholder = ""
textField.delegate = self
}
alert.addAction(defaultAction)
alert.addAction(cancelAction)
self.present(alert, animated: true) {
}
}
Here I check if the user typed in something:
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if (textField.text?.count)! > 0 {
self.alert.actions[0].isEnabled = true
}else{
self.alert.actions[0].isEnabled = false
}
return true;
}
But its still not working.
Thanks in advance for your help!
swift uitextfield alert
swift uitextfield alert
asked Jan 2 at 11:43
jo1995jo1995
185110
185110
your textfield delegate is working or not ?
– Taimoor Suleman
Jan 2 at 11:52
Yes it works. this time, the action is enabled if there is one character inside the textfield but that makes no sense.
– jo1995
Jan 2 at 11:54
Repoen the xcode, clean your porject and run again
– Taimoor Suleman
Jan 2 at 11:55
add a comment |
your textfield delegate is working or not ?
– Taimoor Suleman
Jan 2 at 11:52
Yes it works. this time, the action is enabled if there is one character inside the textfield but that makes no sense.
– jo1995
Jan 2 at 11:54
Repoen the xcode, clean your porject and run again
– Taimoor Suleman
Jan 2 at 11:55
your textfield delegate is working or not ?
– Taimoor Suleman
Jan 2 at 11:52
your textfield delegate is working or not ?
– Taimoor Suleman
Jan 2 at 11:52
Yes it works. this time, the action is enabled if there is one character inside the textfield but that makes no sense.
– jo1995
Jan 2 at 11:54
Yes it works. this time, the action is enabled if there is one character inside the textfield but that makes no sense.
– jo1995
Jan 2 at 11:54
Repoen the xcode, clean your porject and run again
– Taimoor Suleman
Jan 2 at 11:55
Repoen the xcode, clean your porject and run again
– Taimoor Suleman
Jan 2 at 11:55
add a comment |
1 Answer
1
active
oldest
votes
Do this
textField.addTarget(self, action: #selector(self.textEdited), for: .editingChanged)
@objc func textEdited(_ textField:UITextField) {
if textField.text!.count > 0 {
alert.actions.first?.isEnabled = true
}else{
alert.actions.first?.isEnabled = false
}
}
Add this line that will disable it initially
alert.actions.first?.isEnabled = false
self.present(alert, animated: true)
now it works, but only if I type in something and delete it. when the alerts starts I have still the possibility to enter the button, even if there is no content inside the text field. thanks for your help!
– jo1995
Jan 2 at 12:18
see edit.................
– Sh_Khan
Jan 2 at 13:17
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54005741%2fhow-to-enable-disable-alert-action%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Do this
textField.addTarget(self, action: #selector(self.textEdited), for: .editingChanged)
@objc func textEdited(_ textField:UITextField) {
if textField.text!.count > 0 {
alert.actions.first?.isEnabled = true
}else{
alert.actions.first?.isEnabled = false
}
}
Add this line that will disable it initially
alert.actions.first?.isEnabled = false
self.present(alert, animated: true)
now it works, but only if I type in something and delete it. when the alerts starts I have still the possibility to enter the button, even if there is no content inside the text field. thanks for your help!
– jo1995
Jan 2 at 12:18
see edit.................
– Sh_Khan
Jan 2 at 13:17
add a comment |
Do this
textField.addTarget(self, action: #selector(self.textEdited), for: .editingChanged)
@objc func textEdited(_ textField:UITextField) {
if textField.text!.count > 0 {
alert.actions.first?.isEnabled = true
}else{
alert.actions.first?.isEnabled = false
}
}
Add this line that will disable it initially
alert.actions.first?.isEnabled = false
self.present(alert, animated: true)
now it works, but only if I type in something and delete it. when the alerts starts I have still the possibility to enter the button, even if there is no content inside the text field. thanks for your help!
– jo1995
Jan 2 at 12:18
see edit.................
– Sh_Khan
Jan 2 at 13:17
add a comment |
Do this
textField.addTarget(self, action: #selector(self.textEdited), for: .editingChanged)
@objc func textEdited(_ textField:UITextField) {
if textField.text!.count > 0 {
alert.actions.first?.isEnabled = true
}else{
alert.actions.first?.isEnabled = false
}
}
Add this line that will disable it initially
alert.actions.first?.isEnabled = false
self.present(alert, animated: true)
Do this
textField.addTarget(self, action: #selector(self.textEdited), for: .editingChanged)
@objc func textEdited(_ textField:UITextField) {
if textField.text!.count > 0 {
alert.actions.first?.isEnabled = true
}else{
alert.actions.first?.isEnabled = false
}
}
Add this line that will disable it initially
alert.actions.first?.isEnabled = false
self.present(alert, animated: true)
edited Jan 2 at 13:31
answered Jan 2 at 11:57
Sh_KhanSh_Khan
46.1k51432
46.1k51432
now it works, but only if I type in something and delete it. when the alerts starts I have still the possibility to enter the button, even if there is no content inside the text field. thanks for your help!
– jo1995
Jan 2 at 12:18
see edit.................
– Sh_Khan
Jan 2 at 13:17
add a comment |
now it works, but only if I type in something and delete it. when the alerts starts I have still the possibility to enter the button, even if there is no content inside the text field. thanks for your help!
– jo1995
Jan 2 at 12:18
see edit.................
– Sh_Khan
Jan 2 at 13:17
now it works, but only if I type in something and delete it. when the alerts starts I have still the possibility to enter the button, even if there is no content inside the text field. thanks for your help!
– jo1995
Jan 2 at 12:18
now it works, but only if I type in something and delete it. when the alerts starts I have still the possibility to enter the button, even if there is no content inside the text field. thanks for your help!
– jo1995
Jan 2 at 12:18
see edit.................
– Sh_Khan
Jan 2 at 13:17
see edit.................
– Sh_Khan
Jan 2 at 13:17
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54005741%2fhow-to-enable-disable-alert-action%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
your textfield delegate is working or not ?
– Taimoor Suleman
Jan 2 at 11:52
Yes it works. this time, the action is enabled if there is one character inside the textfield but that makes no sense.
– jo1995
Jan 2 at 11:54
Repoen the xcode, clean your porject and run again
– Taimoor Suleman
Jan 2 at 11:55