-1

This is my button i use Swift 5

@IBAction func register(_ sender: Any) {
Print("register"}
}

@IBAction func mybutton1(_ sender: Any) {
Print("my button 1"}
}

@IBAction func mybutton2(_ sender: Any) {
Print("my button 2"}
}

i want to disable mybutton1 & mybutton2 only enable button register, i try but it not work Hope you guys can help me write more code...

override func viewDidLoad() {
                super.viewDidLoad()
let result = "try disable a button"
  if result.count == 10 {
    // everything normal
    } else {
    @IBAction func mybutton1(_ sender: Any) {
     mybutton1(_ sender: Any).isEnabled = false;
     }
    @IBAction func mybutton2(_ sender: Any) {
     mybutton2(_ sender: Any).isEnabled = false;
     }
   }
}
  • PLEASE! Give us something we can duplicate! (Sorry for shouting, it's late.) It seems like you know about the `isEnabled` property from the code you posted. So obviously, there is something going on with `result.count`. Here's my initial thought - put a breakpoint someplace (or, you could post more code so I could duplicate) and see what the count **actually** is. I'd put several breakpoints to be honest - and exactly **WHY** is this code in `viewDidLoad`? –  Jul 17 '21 at 05:01
  • For `mybutton1` and `mybutton2`, you need `@IBOutlet` not `@IBAction`. – aheze Jul 17 '21 at 05:02
  • So when dragging, set the "Connection" to "Outlet" instead of "Action". You can use both `@IBOutlet` and `@IBAction` at the same time - Outlet is for getting a reference to the button so that you can set `isEnabled`. Meanwhile, Action is a function that is called when the button is pressed. – aheze Jul 17 '21 at 05:04
  • I used outlet ok, thank you, But I have to change my command from Action to Outlet there are many errors appearing. My app when open it will check the initial condition to disable the button so i need to put it in viewDidLoad. Is it possible to put all the buttons that I want to hide in a single field or panel? – mật khẩu không đổi Jul 17 '21 at 09:01

1 Answers1

1

you can drag the reference of button to your ViewController and can try something like

 myButton1.isEnabled = false
 myButton2.isEnabled = false
Jok3r
  • 454
  • 3
  • 9
  • xcode report: Value of type '(Any) -> ()' has no member 'isEnabled' – mật khẩu không đổi Jul 17 '21 at 04:51
  • first of all you have to drag the buttons to your ViewController. then you will get two refrences of Buttons. example something like IBOutlet weak var myButton1: UIButton! , IBOutlet weak var myButton2: UIButton! , – Jok3r Jul 17 '21 at 04:54