In Swift, why does IBAction take the button as a parameter if its linked to only that one button? - ios

I'm giving myself a crash course on functions and what I can't seem to understand is when using storyboard to create an IBAction by clicking and dragging from a UIButton, why does it give you the option to create a parameter for the button if the button already corresponds to the IBAction you just created? Why would creating the button as a parameter be helpful?This isn't clear to me yet but I really want to understand.
Thank you in advance.

You can connect more than one button to the same action. The button parameter then tells you which button was tapped.
Even when only connected to one button, the parameter is useful. You may wish to access the button in the action. Having the parameter gives you that direct access. No need to access any outlet property you may or may not have.

Related

Why can't I attach an action to the new Sign In With Apple ASAuthorizationAppleIDButton in a Storyboard?

I'm working on a new codebase that has everything laid out in storyboards and I'm trying to implement the new ASAuthorizationAppleIDButton button as part of the new Sign In With Apple feature.
Reading online it looks like I can add a UIButton to the view and then override the class to be ASAuthorizationAppleIDButton. Easy enough. The weird parts comes when I try and wire that button up to an action in the VC. I can't do it. Even if I go the other way and drag from the button to the VC, the action item is missing from the connection menu.
Has anyone had success adding and interacting with this button from a storyboard? I'd prefer not to redo the entire view just because of this one issue.
ASAuthorizationAppleIDButton is not a button but its a control. And this is stated in that way to avoid misuse of this as a button. And only one action is associated with it which cannot be override. The only control you get is to use the delegate method of
didCompleteWithAuthorization & didCompleteWithError.
So if you want to use/include some extra selector or methods. you can add those in these delegate- methods.

how to create my own radio buttons

I am new to Swift and Xcode so I need help with what I am doing. And what I am doing is this.
I have three buttons one of them starts out checked. and what I want it to do is when I click on another button it checks it and unchecked the previous. I want my own radio buttons
For Radio Buttons there is nothing that comes built in.
You can use SSRadioButtonsController
You can create a controller object and add buttons array to it like
var radioButtonController = SSRadioButtonsController()
radioButtonController.setButtonsArray([button1!,button2!,button3!])
You can also use something like this tutorial.
The standard Apple class that is designed to work with radio button groups is NSMatrix. Specifically you'd create an NSMatrix with mode "NSRadioModeMatrix". NSMatrix takes an NSCell for its list of items. So you can use any built-in NSCell subclass (like NSButton) or anything else custom you would like.
You can manage manually by setBackgroundImage and setImage property. at initial time set rounded image as setBackgroundImage for all three button. At button click event set "bulletin" image by setImage property for selected button and set nil for all other button setImage property.

How to make IBAction (from button pressed on View1) finish BEFORE View2 viewDidLoad happens?

I'm asking this because I haven't found answer or solution on any other topic, sorry if this question was already asked and answered. THIS didn't answer my question, although problem is similar.
I am trying to explain it as possibly good as I can.
I'm having a singleton containing data used in application, let's call it DataStore. I'm trying to change some value in it, let's say DataStore.x, using one of IBActions (by pressing one of multiple buttons in View1). This button is also responsible for changing view to View2, modal link. viewDidLoad function from View2 is using DataStore.x variable.
Problem :
DataStore.x isn't set by IBAction yet when it's being used by viewDidLoad (using NSLog gave me information that value is being set, but too late).
prepareForSegue function would be perfect if I knew inside it which button was pressed, so before I switch views I can put the proper value to DataStore.x.
Thank you for your time.
Karol

How can I post a manufactured event to UIButton

I'm writing a simple iOS 6.1 game. The game involves pressing buttons (OK, it's a tictactoe board, with the cells being UIButtons). The allows the player to choose whether to go first, or whether the computer should go first. If the player tells the computer to go first, I want to set some values, and then fire off the UIButton just as if the user had pressed it.
How can I post an event, or otherwise simulate the action of the button being pressed, to let the rest of the framework do the work of handling the button press?
If there is a better design approach, where I don't have to pretend that the computer has pressed a button, I'm open to hearing about that, instead.
Thank you
Your button will be connected to an action method, typically in your view controller. Just call that method yourself, passing the button as the sender.
Your method will be something like:
-(IBAction)buttonPressed:(UIButton*)sender
{
// Respond to your button press...
}
You'd call it as follows:
[self buttonPressed:self.whicheverButtonYouLike];
You'd need the buttons defined as outlets for this to work.

Send message to a button method w/o pressing the button

Apologies: I may be too tired to be working...
I want to send a message to a button w/o pressing the button. The button has an instance method, defined in the view controller, and properly hooked up and working when the button itself is pressed. The method is "newPuzzle", intended to load a new puzzle when pressed. But I also want to trigger this method during start up, so that a new puzzle is loaded and ready to go, during loadView. So I want to say [?? newPuzzle] in loadView. Am I just missing the proper receiver syntax, or is something more elaborate required to accomplish this? Suggestions, keywords, concepts appreciated.
Check if the IBAction is in the form
( IBAction) newPuzzle:(id) sender
If it is then simply do this to call the function in code(viewWillAppear)
[self newPuzzle:nil]:

Resources