Disable all UIResponderStandardEditActions when UITextfield is empty












0















I am currently trying to prevent all UIResponderStandardEditActions like copy, paste, delete from showing up when the UITextfield is empty. I would only like to show them if the user has types a message. I have tried 2 solutions and currently don't work, I'm not sure if its to do with iOS 12 or. I have tried overriding the canPerformAction method both in a UITextfield extension and using a custom class later assigned to the UITextfield in the Storyboard but no luck. Is there another way to do this. Here is what I have tried.



extension UITextField {
open override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
if self.text!.isEmpty {
return false
}

return action == #selector(UIResponderStandardEditActions.paste(_:))
}
}

class CustomTextField: UITextField {
override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
if action == #selector(UIResponderStandardEditActions.paste(_:)) || action == #selector(UIResponderStandardEditActions.copy(_:)) || action == #selector(UIResponderStandardEditActions.delete(_:)) {
return false
}
return true
}
}









share|improve this question



























    0















    I am currently trying to prevent all UIResponderStandardEditActions like copy, paste, delete from showing up when the UITextfield is empty. I would only like to show them if the user has types a message. I have tried 2 solutions and currently don't work, I'm not sure if its to do with iOS 12 or. I have tried overriding the canPerformAction method both in a UITextfield extension and using a custom class later assigned to the UITextfield in the Storyboard but no luck. Is there another way to do this. Here is what I have tried.



    extension UITextField {
    open override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
    if self.text!.isEmpty {
    return false
    }

    return action == #selector(UIResponderStandardEditActions.paste(_:))
    }
    }

    class CustomTextField: UITextField {
    override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
    if action == #selector(UIResponderStandardEditActions.paste(_:)) || action == #selector(UIResponderStandardEditActions.copy(_:)) || action == #selector(UIResponderStandardEditActions.delete(_:)) {
    return false
    }
    return true
    }
    }









    share|improve this question

























      0












      0








      0








      I am currently trying to prevent all UIResponderStandardEditActions like copy, paste, delete from showing up when the UITextfield is empty. I would only like to show them if the user has types a message. I have tried 2 solutions and currently don't work, I'm not sure if its to do with iOS 12 or. I have tried overriding the canPerformAction method both in a UITextfield extension and using a custom class later assigned to the UITextfield in the Storyboard but no luck. Is there another way to do this. Here is what I have tried.



      extension UITextField {
      open override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
      if self.text!.isEmpty {
      return false
      }

      return action == #selector(UIResponderStandardEditActions.paste(_:))
      }
      }

      class CustomTextField: UITextField {
      override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
      if action == #selector(UIResponderStandardEditActions.paste(_:)) || action == #selector(UIResponderStandardEditActions.copy(_:)) || action == #selector(UIResponderStandardEditActions.delete(_:)) {
      return false
      }
      return true
      }
      }









      share|improve this question














      I am currently trying to prevent all UIResponderStandardEditActions like copy, paste, delete from showing up when the UITextfield is empty. I would only like to show them if the user has types a message. I have tried 2 solutions and currently don't work, I'm not sure if its to do with iOS 12 or. I have tried overriding the canPerformAction method both in a UITextfield extension and using a custom class later assigned to the UITextfield in the Storyboard but no luck. Is there another way to do this. Here is what I have tried.



      extension UITextField {
      open override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
      if self.text!.isEmpty {
      return false
      }

      return action == #selector(UIResponderStandardEditActions.paste(_:))
      }
      }

      class CustomTextField: UITextField {
      override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
      if action == #selector(UIResponderStandardEditActions.paste(_:)) || action == #selector(UIResponderStandardEditActions.copy(_:)) || action == #selector(UIResponderStandardEditActions.delete(_:)) {
      return false
      }
      return true
      }
      }






      ios swift uitextfield uiresponder






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 20 '18 at 9:41









      mandem112mandem112

      507




      507
























          1 Answer
          1






          active

          oldest

          votes


















          0














          Just override with your subclass and use this class instead of UITextField. By the way, this will disable copy paste,cut for whole conditions so that you need to add some hasText: Bool or related condition to the switch case.



          @IBDesignable
          class ActionsDisabledUITextField: UITextField {

          @IBInspectable var isPasteEnabled: Bool = false

          @IBInspectable var isSelectEnabled: Bool = false

          @IBInspectable var isSelectAllEnabled: Bool = false

          @IBInspectable var isCopyEnabled: Bool = false

          @IBInspectable var isCutEnabled: Bool = false

          @IBInspectable var isDeleteEnabled: Bool = false

          override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
          switch action {
          case #selector(UIResponderStandardEditActions.paste(_:)) where !isPasteEnabled,
          #selector(UIResponderStandardEditActions.select(_:)) where !isSelectEnabled,
          #selector(UIResponderStandardEditActions.selectAll(_:)) where !isSelectAllEnabled,
          #selector(UIResponderStandardEditActions.copy(_:)) where !isCopyEnabled,
          #selector(UIResponderStandardEditActions.cut(_:)) where !isCutEnabled,
          #selector(UIResponderStandardEditActions.delete(_:)) where !isDeleteEnabled:
          return false
          default:
          //return true : this is not correct
          return super.canPerformAction(action, withSender: sender)
          }
          }
          }





          share|improve this answer
























          • This doesn't work mate

            – mandem112
            Nov 20 '18 at 10:56











          • What is not working? I'm using this class in my more than one project. How did you implement it? Can you please share some detail?

            – Emre Önder
            Nov 20 '18 at 11:32











          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
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53390122%2fdisable-all-uiresponderstandardeditactions-when-uitextfield-is-empty%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









          0














          Just override with your subclass and use this class instead of UITextField. By the way, this will disable copy paste,cut for whole conditions so that you need to add some hasText: Bool or related condition to the switch case.



          @IBDesignable
          class ActionsDisabledUITextField: UITextField {

          @IBInspectable var isPasteEnabled: Bool = false

          @IBInspectable var isSelectEnabled: Bool = false

          @IBInspectable var isSelectAllEnabled: Bool = false

          @IBInspectable var isCopyEnabled: Bool = false

          @IBInspectable var isCutEnabled: Bool = false

          @IBInspectable var isDeleteEnabled: Bool = false

          override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
          switch action {
          case #selector(UIResponderStandardEditActions.paste(_:)) where !isPasteEnabled,
          #selector(UIResponderStandardEditActions.select(_:)) where !isSelectEnabled,
          #selector(UIResponderStandardEditActions.selectAll(_:)) where !isSelectAllEnabled,
          #selector(UIResponderStandardEditActions.copy(_:)) where !isCopyEnabled,
          #selector(UIResponderStandardEditActions.cut(_:)) where !isCutEnabled,
          #selector(UIResponderStandardEditActions.delete(_:)) where !isDeleteEnabled:
          return false
          default:
          //return true : this is not correct
          return super.canPerformAction(action, withSender: sender)
          }
          }
          }





          share|improve this answer
























          • This doesn't work mate

            – mandem112
            Nov 20 '18 at 10:56











          • What is not working? I'm using this class in my more than one project. How did you implement it? Can you please share some detail?

            – Emre Önder
            Nov 20 '18 at 11:32
















          0














          Just override with your subclass and use this class instead of UITextField. By the way, this will disable copy paste,cut for whole conditions so that you need to add some hasText: Bool or related condition to the switch case.



          @IBDesignable
          class ActionsDisabledUITextField: UITextField {

          @IBInspectable var isPasteEnabled: Bool = false

          @IBInspectable var isSelectEnabled: Bool = false

          @IBInspectable var isSelectAllEnabled: Bool = false

          @IBInspectable var isCopyEnabled: Bool = false

          @IBInspectable var isCutEnabled: Bool = false

          @IBInspectable var isDeleteEnabled: Bool = false

          override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
          switch action {
          case #selector(UIResponderStandardEditActions.paste(_:)) where !isPasteEnabled,
          #selector(UIResponderStandardEditActions.select(_:)) where !isSelectEnabled,
          #selector(UIResponderStandardEditActions.selectAll(_:)) where !isSelectAllEnabled,
          #selector(UIResponderStandardEditActions.copy(_:)) where !isCopyEnabled,
          #selector(UIResponderStandardEditActions.cut(_:)) where !isCutEnabled,
          #selector(UIResponderStandardEditActions.delete(_:)) where !isDeleteEnabled:
          return false
          default:
          //return true : this is not correct
          return super.canPerformAction(action, withSender: sender)
          }
          }
          }





          share|improve this answer
























          • This doesn't work mate

            – mandem112
            Nov 20 '18 at 10:56











          • What is not working? I'm using this class in my more than one project. How did you implement it? Can you please share some detail?

            – Emre Önder
            Nov 20 '18 at 11:32














          0












          0








          0







          Just override with your subclass and use this class instead of UITextField. By the way, this will disable copy paste,cut for whole conditions so that you need to add some hasText: Bool or related condition to the switch case.



          @IBDesignable
          class ActionsDisabledUITextField: UITextField {

          @IBInspectable var isPasteEnabled: Bool = false

          @IBInspectable var isSelectEnabled: Bool = false

          @IBInspectable var isSelectAllEnabled: Bool = false

          @IBInspectable var isCopyEnabled: Bool = false

          @IBInspectable var isCutEnabled: Bool = false

          @IBInspectable var isDeleteEnabled: Bool = false

          override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
          switch action {
          case #selector(UIResponderStandardEditActions.paste(_:)) where !isPasteEnabled,
          #selector(UIResponderStandardEditActions.select(_:)) where !isSelectEnabled,
          #selector(UIResponderStandardEditActions.selectAll(_:)) where !isSelectAllEnabled,
          #selector(UIResponderStandardEditActions.copy(_:)) where !isCopyEnabled,
          #selector(UIResponderStandardEditActions.cut(_:)) where !isCutEnabled,
          #selector(UIResponderStandardEditActions.delete(_:)) where !isDeleteEnabled:
          return false
          default:
          //return true : this is not correct
          return super.canPerformAction(action, withSender: sender)
          }
          }
          }





          share|improve this answer













          Just override with your subclass and use this class instead of UITextField. By the way, this will disable copy paste,cut for whole conditions so that you need to add some hasText: Bool or related condition to the switch case.



          @IBDesignable
          class ActionsDisabledUITextField: UITextField {

          @IBInspectable var isPasteEnabled: Bool = false

          @IBInspectable var isSelectEnabled: Bool = false

          @IBInspectable var isSelectAllEnabled: Bool = false

          @IBInspectable var isCopyEnabled: Bool = false

          @IBInspectable var isCutEnabled: Bool = false

          @IBInspectable var isDeleteEnabled: Bool = false

          override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
          switch action {
          case #selector(UIResponderStandardEditActions.paste(_:)) where !isPasteEnabled,
          #selector(UIResponderStandardEditActions.select(_:)) where !isSelectEnabled,
          #selector(UIResponderStandardEditActions.selectAll(_:)) where !isSelectAllEnabled,
          #selector(UIResponderStandardEditActions.copy(_:)) where !isCopyEnabled,
          #selector(UIResponderStandardEditActions.cut(_:)) where !isCutEnabled,
          #selector(UIResponderStandardEditActions.delete(_:)) where !isDeleteEnabled:
          return false
          default:
          //return true : this is not correct
          return super.canPerformAction(action, withSender: sender)
          }
          }
          }






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 20 '18 at 10:26









          Emre ÖnderEmre Önder

          834524




          834524













          • This doesn't work mate

            – mandem112
            Nov 20 '18 at 10:56











          • What is not working? I'm using this class in my more than one project. How did you implement it? Can you please share some detail?

            – Emre Önder
            Nov 20 '18 at 11:32



















          • This doesn't work mate

            – mandem112
            Nov 20 '18 at 10:56











          • What is not working? I'm using this class in my more than one project. How did you implement it? Can you please share some detail?

            – Emre Önder
            Nov 20 '18 at 11:32

















          This doesn't work mate

          – mandem112
          Nov 20 '18 at 10:56





          This doesn't work mate

          – mandem112
          Nov 20 '18 at 10:56













          What is not working? I'm using this class in my more than one project. How did you implement it? Can you please share some detail?

          – Emre Önder
          Nov 20 '18 at 11:32





          What is not working? I'm using this class in my more than one project. How did you implement it? Can you please share some detail?

          – Emre Önder
          Nov 20 '18 at 11:32


















          draft saved

          draft discarded




















































          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.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53390122%2fdisable-all-uiresponderstandardeditactions-when-uitextfield-is-empty%23new-answer', 'question_page');
          }
          );

          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







          Popular posts from this blog

          android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

          SQL update select statement

          'app-layout' is not a known element: how to share Component with different Modules