UISegmentedControl to control segues - ios

Hi I am creating an application which will switch between bluetooth and wifi mode. I want to use a UISegmentedControl to switch between the segues. The photo album I will post is an example of what I want to achieve, although that was done with .xibs.
I've already tried using the IBAction method for the segment and then using the performSegueWithIdentifier method. The latter only works one way and not vice-versa, which is what I need. Also even though it works for one way, I still get an error.
http://imgur.com/a/pTTsu
To clarify I want to be able to toggle between the two screens, but I am only able to switch from screen 1 to 2.

If you have two different segues, and you want to perform one based on what was pressed then just check which index is selected and perform that segue:
- (IBAction)yourSegmentedControlPressed:(UISegmentedControl *)sender
{
if(sender.selectedSegmentIndex==0)//left control button pressed
{
[self performSegueWithIdentifier:#"yourFirstSegue" sender:self];
}
else if(sender.selectedSegmentIndex==1)//right control button pressed
{
[self performSegueWithIdentifier:#"yourSecondSegue" sender:self];
}
}
If you want to use another button to control the segues, then simply just grab the segmentedControl outlet and check the index inside of that action. NOTE: the index will be equal to -1 if neither is selected.

Related

iOS IBAction with textDocumetProxy within a second ViewController (Objective-C)

I don't have a ton of experience with iOS development, but I feel like this should be possible. What I'm trying to do is do an IBAction within a second view controller. The IBAction is I'm trying to preform looks like this:
- (IBAction)helloWorldButton:(id)sender {
[self.textDocumentProxy insertText:#"Hello World"];
}
I've linked up the send event to touch up inside. I am able to pull up the second view controller and properly dismiss it with:
- (IBAction)dismissSecondView:(id)sender {
[[self presentingViewController] dismissViewControllerAnimated:NO completion:nil];
}
That all works fine which is what's confusing me. Why does the IBAction for dismissing the view controller work but the IBAction for textDocumentProxy doesn't? I can provide more code and information if needed, but I have successfully linked up the two view controllers to appear and be dismissed. I just want a button that types "Hello World" within the secondary view controller.
Thank you for your help in advance!
I figured it out. I was thinking about it all wrong. What I was looking to do was change the label on multiple buttons, not change the entire view. What I did was this:
Take the button to change the state of other buttons, and tell it tell it to change the buttons accordingly using
- (IBAction)buttonChanger:(id)sender {
if ([_aButton.titleLabel.text isEqual: #"Hello"]) {
[UIView setAnimationsEnabled:NO];
[_anotherButton setTitle:#"World" forState:UIControlStateNormal];
[_lastButton setTitle:#"Friend!" forState:UIControlStateNormal]; //more buttons after this...
Not sure if that example makes a ton of sense (changed the variables up a bit from the original project) but that will change each button accordingly. When I push button "Button Changer", it checks the status of "aButton" to see if anything needs changing, and then changes the title of the other buttons (anotherButton, lastButton) to World and Friend! accordingly. This is exactly what I was looking to do. I realize this is a completely different route to what I was initially thinking, but it works great!
Thanks for your help!

Return to main menu on Long press of a Switch Access button

I am creating a game specifically designed to be played using Switch access. The game builds a grid of buttons dynamically based on the level to mimic the grid layout of an iPad's icons. With the nature of switch access I cannot have a back button to return to the main menu as this will get counted within the game as a button and included in the tabbing through the user will be doing.
Ideally I would like the user to be able to return to the main menu on a long press of one of the switch buttons, I have tried attaching a Long press gesture recogniser onto the view that the buttons were drawn to, this worked fine in the simulator but not with the switch access button press, I then tried to attach it to each button, again this worked in the simulator but not in actual testing.
Here is the code I am using which works in the simulator.
Adding UILongPressGestureRecognizer to the button.
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(UILongPressBack:)];
[self.button addGestureRecognizer:longPress];
Method called on longpress.
- (IBAction)UILongPressBack:(UILongPressGestureRecognizer*)sender {
if (sender.state == UIGestureRecognizerStateEnded) {
MainViewController * mainPage = [[MainViewController alloc]init];
[self presentViewController:mainPage animated:YES completion:nil];
}
}
From what I understand the switch access controls work just like a bluetooth keyboard (which is how I am testing at the moment). When setting up the switch access buttons I map each one to a certain keyboard key.
Does anyone know how I can get this working? I had considered disabling a button while the game is in play and re-enabling it between levels but then the user doesn't have the option to return the main menu mid game if they wish, which I feel would be bad design.
Thanks for any help.
In scanning mode, Switch Control sequentially focuses elements. Selecting an element using a switch press activates it. However, various user settings govern the speed and criteria for selection. Your app can only respond to UI and accessibility events– there's nothing you can or, in most cases, should do to to infer the behaviors that generated these events.
Adding a custom action
If you would like to offer an action unique to users of assistive technologies, implement -accessibilityCustomActions. This allows users to choose from a set of application-defined actions in addition to the system-standard activate action.
Code
For example, to expose and respond to a custom action foo on an otherwise bare UIView, one can implement:
- (BOOL)isAccessibilityElement
{
return YES;
}
- (NSArray *)accessibilityCustomActions
{
UIAccessibilityCustomAction *fooAction = [[UIAccessibilityCustomAction alloc] initWithName:#"FooAction" target:self selector:#selector(foo)];
return #[fooAction];
}
- (void)foo
{
NSLog(#"foo");
}
Output
Running the code above with Switch Control enabled, you see a menu item for the new action:
Edit:
In the comments, you ask how this could be implemented without a menu since it requires additional switch presses to access, which affects gameplay. There is another approach. You can display a menu button that is explicitly marked not accessible (isAccessibilityElement = NO). This button will not be in the scan order, but Switch Control users can still tap it by point scanning with the "Gliding Cursor" instead of item scanning. As usual, I would discourage doing something non-standard like that unless you're absolutely certain of your users' abilities.

Animation while delete or reorder the cell in table view

I would like to implement an animation when a user tries to delete and reorder a cell. Now it is coming instantly when I click on the edit button. Can you help me to make this code as an iphone contact app animation.
-(IBAction)edit:(id)sender
{
tabView.editing = !tabView.editing;
}
Take a look at UITableView class reference and you should be able to find the instance method you need.
The instance method setEditing:animated: will be able to do what you want. The second parameter will give the animation transition if you set it to YES.
For example:
[tabView setEditing:!tabView.editing animated:YES];

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.

Event fired two times when clicked to button in my iOS. app

I made a simple IOS. application where the UI contains buttons. For example I have nine buttons each button represent a number same as numeric keyboard. I made this buttons the following way, put it to the storyboard and made the Touch Up Inside event with ctrl drag functions, after I copied the buttons eight times.
The problem is when I pressed a button the event comes sometimes twice. It happens randomly. I put a break point into line NSLog I did not see faulty thing at all.
- (IBAction)digitPressed:(UIButton *)sender {
if ([lastPressed isEqualToString:[sender currentTitle]]) {
NSLog(#"Douple pressed digit"); // break it here
}
// store to lastPressed
lastPressed = [sender currentTitle];
}
If you get multiple times in digitPressed when clicking once I would guess something went wrong when you copied the buttons so check the wiring in your storyboard.
And as Vince said it's a good practice to use id, it's not because this method gets triggered by buttons now that they should always be triggered by a button.
It's easier to debug when you log every time instead of only doubles.
NSLog(#"Button pressed: %#", [sender currentTitle]);

Resources