Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

219
Views
Acceder al elemento del menú para desactivar/activar

Me gustaría implementar NSMenuItem para activar ciertas funciones (por ejemplo, "Ejecutar cálculo"). ¿Cómo accedo a los elementos del menú para habilitar o deshabilitar los elementos según la lógica de la aplicación? Por ejemplo, la función "cortar" para el texto solo se activa como elemento de menú cuando se selecciona la prueba. "Ejecutar cálculo" solo debe habilitarse cuando se dan ciertos criterios. ¡Gracias!

ingrese la descripción de la imagen aquí

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Probablemente tenga algún controlador de vista o controlador de ventana que implemente runCalculation , como este:

 class ViewController: NSViewController { @IBAction func runCalculation(_ sender: Any?) { print(1 + 1) } }

Y ha conectado la acción del elemento de menú "Ejecutar cálculo" al método runCalculation cálculo del controlador.

Para habilitar y deshabilitar el elemento del menú, siga estos pasos:

  1. Asegúrese de que el menú "Calculadora" en sí (del tipo NSMenu ) tenga activada la propiedad "Auto Enable Items" en IB, o que tenga autoenablesItems configurado como verdadero en el código.

    casilla de verificación de elementos de habilitación automática del menú

  2. Haga que su controlador se ajuste al protocolo NSUserInterfaceValidations :

     extension ViewController: NSUserInterfaceValidations { func validateUserInterfaceItem(_ item: NSValidatedUserInterfaceItem) -> Bool { // See step 3... return true } }
  3. En validateUserInterfaceItem , verifica si la action del elemento es runCalculation(_:) . Si es así, devuelva verdadero si y solo si desea permitir que el usuario ejecute el cálculo:

     extension ViewController: NSUserInterfaceValidations { func validateUserInterfaceItem(_ item: NSValidatedUserInterfaceItem) -> Bool { switch item.action { case #selector(runCalculation(_:))?: // Put your real test here. return !textField.stringValue.isEmpty default: return true } } }
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!