How can I post a manufactured event to UIButton - ios

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.

Related

Xcode program: Text box that the user can interact with through touch

I am new to Xcode and Swift.
I want to create a text box for a video game that acts as the dialogue between the game and the user. for example I want the text box to say (character name) walked 10 paces or "Used 'detect evil' orc within 60'. "
I also want the ability for the user to pressure touch this chat text message which is sent from the computer and then do a series of actions. For example if a paladin detects evil maybe the next course of action would be to hide or prepare to fight. Any ideas on how to set this up? I have been trying to create this on Main.storyboard. Can I only achieve this through the ViewController or one of the delegates?
First of all, you can only achieve this by coding (by using Storyboards as your ally of course!)
If you want to have a text box that is "clickable", what first comes to mind is just a button, that have no background color, so it looks just like text.
Then you need to create a method inside of your ViewController, ideally something like:
#IBAction func dialogBoxTapped(_ sender: Any?) {
// your code goes here
}
And connect your button to this function by setting up a touchUpInside method in the Storyboard.

How do I toggle hidden of a label while a button is pressed?

I am trying to figure out how to only display a label while a button is pressed in OS. I know how to operate the touch events but I am not sure how to incorporate the UILongPressGestureRecognizer into this.
The UIButton class, as well as lots of other UIControl subclasses can have numerous actions hooked up to them.
When we are hooking up an action from interface builder to our source code file, if we open the "Event" drop down, we're presented with a long list of options:
In almost every scenario, we're hooking our actions only up to "Touch Up Inside". This allows the user to consider whether or not they want to really press the button. If they drag their finger off the button before letting go, the action doesn't fire, because the "up touch" gesture happened outside the bounds of the object.
But here, we want to actually hook our button's "touch down" event up. This is when we'll display the label.
Let's go ahead and create a "touch down" event and a "touch up inside" event:
Swift
#IBAction func buttonTouchDown(sender: UIButton) {
self.myLabel.hidden = false
}
#IBAction func buttonTouchEnded(sender: UIButton) {
self.myLabel.hidden = true
}
Objective-C
- (IBAction)buttonTouchDown:(UIButton *)sender {
self.myLabel.hidden = NO;
}
- (IBAction)buttonTouchEnded:(UIButton *)sender {
self.myLabel.hidden = YES;
}
So far, buttonTouchEnded is set up completely normally, and buttonTouchDown was set up by selecting "touch down" from the "Event" list.
We can always verify what our control is hooked up to by right clicking it in the interface builder:
But this menu is useful for more than simply checking what we've already hooked up. From here, we can hook up any of the other actions to our existing #IBAction methods simply by clicking in the circle and dragging to the existing method.
So we obviously want the label to disappear if we stop pressing the button, a normal touch up like you'd hook up any other button. The only question remaining is, what exact behavior do you want?
If you want the label to disappear only when the finger is lifted, no matter where the finger goes, then we must also hook up "touch up outside".
If you want the label to disappear when the user drags their finger off the button, then we should hook up the "touch drag exit" action.
We also probably want to hook up the "touch cancel" action, which would occur if some sort of system event (perhaps an incoming phone call) cancels the touch.
This Stack Overflow answer elaborates on the differences between the action options we have, so you can craft the behavior exactly how you need it.
Anyway, once we decide which actions we want to hook up to which methods, bring up that right click menu and click-drag from the circles to the methods:
The easiest thing to do would be to add an action to the touchDown event and a separate action to touchUpInside and touchUpOutside.
Show the label on the touchDown action and hide it on the touchUpInside / touchUpOutside action. (and for completeness, on touchCancel, as suggested by nhgrif in his very thorough answer.)
A long press gesture recognizer won't work in this situation. You could create a custom gesture recognizer that triggered one event on touch and another event on release, and use that. It's actually not that hard to do.
EDIT
I just uploaded a demo project to GitHub called "MorphingButton" (link) that I created for another question here on Stack Overflow.
That project now shows a label on touching the app button and hides the label when you release the button.
The project is a hybrid Swift/Objective-C project that shows how to do the button morphing and label showing/hiding in both languages. It has a tab bar with a Swift tab and an Objective-C tab.

What are the "First Responder" and "Exit" boxes purpose in the storyboard editor?

In the XCode IDE, at the bottom of the view controller in the MainStoryboard editor, are two boxes: First Responder, and Exit.
I know what a firstResponder is programatically within the code, but in the storyboard editor, I can't seem to do anything useful by it.
Am I able to use the first responder in this area to somehow set the first responder of the view? I'd like the first textfield to be active on load and I have tried right+click and dragging to no avail. I know I can set it programatically in the viewDidLoad method, but is there some way of doing it here?
And what is the green Exit for?
There are no good answer for this question, so I am posting my answer:
From here:
Note: You probably won’t be using the First Responder very much. This is a proxy object that refers to whatever object has first responder status at any given time. It was also present in your nibs and you probably never had a need to use it then either. As an example, you can hook up the Touch Up Inside event from a button to First Responder’s cut: selector. If at some point a text field has input focus then you can press that button to make the text field, which is now the first responder, cut its text to the pasteboard.
Edit:
1) First Responder is very useful if you are using text fields with keyboard notifications. I use it to make keyboard disappear, make an outlet to variable currentFirstResponder of your class, and in viewWillDisappear:
[self.currentFirstResponder resignFirstResponder];
2) You can read about unwind segues ("Exit" box) here
I've never used it and probably never will but you can assign an object to be the first in line to receive the events from the UI.
I suppose you could be creating a UIView subclass and add it in to a UIViewController but you actually want some other object to receive and process the events other than the UIViewController you are adding it to.
I found this link which kind of explains it a bit better.
First Responder: The First Responder icon stands for the object that the user is currently interacting with. When a user works with an iOS application, multiple objects could potentially respond to the various gestures or keystrokes that the user creates. The first responder is the object currently in control and interacting with the user. A text field that the user is typing into, for example, would be the first responder until the user moves to another field or control.
Exit: The Exit icon serves a very specific purpose that will come into play only in multiscene applications. When you are creating an app that moves the user between a series of screens, the Exit icon provides a visual means of jumping back to a previous screen. If you have built five scenes that link from one to another and you want to quickly return to the first scene from the fifth, you’ll link from the fifth scene to the first scene’s Exit icon.
More here
You don't see this very often, where a deleted answer is actually correct, and the comment (likely influencing its deletion) on it is totally wrong! I'll try and improve on it.
Usually the IBAction you want to hook up to a button is in the view controller containing the button. However if the IBAction is in a different controller, e.g. a parent controller then drag from the button to the First Responder object and you are able to select the IBAction in the parent controller!
As the hidden answer states, how this is implemented is the action is sent to nil, which has the effect of the responder chain (i.e. view hierarchy) being searched for the action, as follows:
[UIApplication.sharedApplication sendAction:#selector(nextObject:) to:nil from:self forEvent:nil];
An example is a custom UITableViewCell. Add a UIButton to the cell but you want the action to go up to a View Controller that has an embed segue to a UITableViewController. Drag the touch up instead action to the First Responder and select the action in the container view controller. In the action to find the indexPath simply loop the visibleCells and check if the sender is isDescendantOfView:
- (IBAction)cellButtonTapped:(id)sender{
for(UITableViewCell *cell in self.tableViewController.tableView.visibleCells){
if([sender isDescendantOfView:cell]){
NSIndexPath *indexPath = [self.tableViewController.tableView indexPathForCell:cell];
NSLog(#"tapped %#", indexPath);
}
}
}
Another example could be a reload button: say your first view controller shows an downloaded item with an IBAction to reload it to get the latest data, then your child controller shows some detail, but you also want them to be able to reload the main item from within the detail, just add a button in the detail and drag its action to First Responder and select the reload IBAction in the parent controller. This allows you to hook up buttons to parent actions with no additional code like delegate methods!
For this to work the action needs to be in the responder chain hierarchy or it won't be found, you can read how the chain is built up in the docs. Also note if called from code the view needs to have appeared, viewWillAppear is too soon.

UIButton's click action?

I am following a book to learn IOs programming. For UIButton, it supposed to have a click action. However I only saw some actions like touch down etc.
Also, want to know why the default action not the "touch down" but "touchUpInside"
I am using XCode 4.3.
click action in iOS is represented but touchUpInside, since click is a mouse event
touchUpInside, means that the user touched down on a button, and then touch up, while he still is on the same button, which is the normal behavior when you want to tap on a button
In storyboard, you can right click on the UIButton and see the list of events available for a UIButton and choose appropriate event based on your requirement.
For a click like action you can use Touch Up Inside event.

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