Hide one view and unhide another upon touching a button - ios

I have been creating "if/then" apps for android and now my boss want me to do the same for his iPad. I just need to figure out how to code so that when the buttons are clicked, it hides the current view (text and button) and reveals the next set of text and buttons.

Make sure your two sets of text/buttons are in two UIViews (I will refer to these as 'viewOne' and 'viewTwo'), when you want to swap your views, use this code:
[viewOne setHidden:[viewTwo isHidden]];
[viewTwo setHidden:![viewTwo isHidden]];
It's not the most understandable way to do this, but it's one of the shortest.
For something easier to read:
if ([viewOne isHidden]) {
[viewOne setHidden:NO];
[viewTwo setHidden:YES];
} else {
[viewOne setHidden:NO];
[viewTwo setHidden:YES];
}
Either will work, it just depends on how you like to write your code.

Related

iOS: showing/hiding UI elements dynamically

I have an app with a UITableView of results in a center column, and a little search bar at the top. I want to dynamically add/remove a button that says "reset search" and pin it to the top of the view.
There are a couple ways to go about it, and I'm worried that they both look ugly or hacky to me. To wit:
Add the button in the storyboard editor, and show/hide it in code. The trouble is I've already got a bunch of views specified this way in the storyboard, and so positioning/selecting them is a huge pain since they overlap each other.
Add the button in code. Except now my UI is specified in two places: the stuff that's in the storyboard, and the additional modifications that take place in the code.
What's the standard way of doing something like this? And how can I prevent my storyboards from becoming a big mess when I've got buttons/dialogs/etc. that need to be dynamically shown/hidden?
Well my first answer is to not use storyboards in the first place. However, I understand that's not helpful in this case.
If I were you, I would do option 2. It's a one off for this single button and it has a specific use case. It doesn't hurt to specify it in code. The following is for the
.h
#property (nonatomic, strong) UIButton *resetButton;
And
.m
//I'm guessing you're using a VC, so I'd put this in viewDidLoad
self.resetButton = [[UIButton alloc]initWithFrame:YOUR FRAME];
self.resetButton.alpha = 0.0;
//any other styling
[self.view addSubview:self.resetButton];
self.resetButton addTarget:self action:#selector(onReset) forControlEvents:UIControlEventTouchUpInside];
//and then add these three methods
- (void)onReset {
//called when reset button is tapped
}
- (void)showResetButton {
[UIView animateWithDuration:.3 animations:^{
self.resetButton.alpha = 1.0;
}];
}
- (void)hideResetButton {
[UIView animateWithDuration:.3 animations:^{
self.resetButton.alpha = 0.0;
}];
}
I don't know if I have understood, but if you want to hide an object with an action, you can do so :
- (IBAction)myaction:(id)sender
{
self.object1.hidden = false ;
self.object2.hidden = true ;
self.object3.hidden = false ;
}
Both ways are perfect, I personally prefer the Storyboard one because it lets you arrange the button more easily and it's easier to add constraints for auto-layout(if needed) in Interface Builder than in code.
For your second question: If your storyboard is cluttered and views are all over the place, I would suggest that you select your views from the side bar, rather thank trying to click on them. Also, if you want to move the selected view, adjust the coordinates in the Utilities panel instead of dragging it with the mouse.

How to set visibility of a group of views in iOS

I need to show one view or another in a single function.
Is there any way to do this without doing:
[label1 setHidden:YES];
[label2 setHidden:YES];
[label3 setHidden:YES];
e.g. in one function?
In android I would create two absolute layouts and show one or the other, I am searching something similar on iOS.
You can add those UILabel inside a UIView, and then when you need them to hide, you can set that UIView to be hidden.
You hide all subviews in a single line.
[view.subviews makeObjectsPerformSelector:#selector(setHidden:)
withObject:[NSNumber numberWithBool:YES]];
Similarly if you want to remove all subviews you can remove them in single line
[view.subviews makeObjectsPerformSelector:#selector(removeFromSuperView)];

Unable to hide UIButton

I have a view that is accessible in two different ways. I have an if statement that determines in which case a button should be displayed.
if([Recipes entryExists:[note recipeIdentifier]]){
[buttons insertObject:btnRemoveFave atIndex:0];
[btnPrefs setHidden:NO];
} else {
[buttons insertObject:btnAddFave atIndex:0];
[btnPrefs setHidden:YES];
[btnPrefs setEnabled:NO];
}
I have placed a break point in the in both conditions of the if statement. When code enters the else condition, the lines that 'setHidden' and 'setEnabled' ARE both executed, yet the button is still visible AND enabled.
Any ideas as to why I can't disable the button? Thanks!
Can you check if you are creating a new instance of the button each time you call the statement?
If you are using a local variable instead of instance, use the tag property to identify your button, so you can find it in the buttons array.
Besides that, I prefer using alpha=0.0 instead of hidden=YES.
Good luck.
Use below code. It's working for me.
For Remove:
[btnPrefs removeFromSuperview];
Then Add:
[self.view addSubview:btnPrefs];

UIButton at bottom of screen only intermittently highlights

I have a few UIButtons at the bottom of my app's main view. These buttons intermittently don't highlight when a user taps them but their target methods always get called. I've discovered it's Control Center's gesture recognizer getting in the way of UIButton's highlighting. If I move the containing view up toward the middle of the screen everything functions as designed.
The issue is reported here https://devforums.apple.com/message/865922
As a workaround I've tried setting the highlighted state by hand with the target method. This seems to have the same effect of allowing the UIButton to highlight normally.
Any ideas how to work around this without redesigning these controls to appear elsewhere in the app?
Perhaps I use a standard view and add all the methods for touch interaction by hand? How would I do that? Is it even worth exploring?
I've found a pretty simple workaround for this. Using standard properties like .highlighted = YES and .selected = YES doesn't seem to work within that bottom band. Instead of setting the highlighted state, I just set the background image of the button to the highlighted state with an unperceived delay BEFORE we call the final method.
[self.stopRecordingButton setImage:[UIImage imageNamed:#"stopRecordingButton"] forState:UIControlStateNormal];
[self.stopRecordingButton setImage:[UIImage imageNamed:#"stopRecordingButton-highlighted"] forState:UIControlStateHighlighted];
[self.stopRecordingButton addTarget:self action:#selector(stopRecordingDelay) forControlEvents:UIControlEventTouchUpInside];
-(void)stopRecordingDelay
{
[self.stopRecordingButton setImage:[UIImage imageNamed:#"stopRecordingButton-highlighted"] forState:UIControlStateNormal];
[self performSelector:#selector(stopRecording) withObject:nil afterDelay:0.025f];
}
- (void)stopRecording
{
[self.stopRecordingButton setImage:[UIImage imageNamed:#"stopRecordingButton"] forState:UIControlStateNormal];
//Do real stuff
}
I recently ran into the same problem and search everywhere for an answer. This is what worked for me. It was a combination of two things, the UINavigationController back swipe gesture and the iOS 7 control center gesture (up swipe from bottom of the screen).
Disable the back swipe gesture if on a UINavigationController:
in viewDidLoad:
if ([self.navigationController respondsToSelector:#selector(interactivePopGestureRecognizer)]) {
self.navigationController.interactivePopGestureRecognizer.enabled = NO;
}
Set the control center gesture to only show an up arrow instead of showing the control center first. You can do this by overriding the following UIViewController method:
- (BOOL)prefersStatusBarHidden {
return YES;
}
Hope this helps!
I've posted a fix for this issue that brings the highlight back described in this question. The highlight is fixed by subclassing UIButton and overriding pointInside: to catch touch events
If you have a button that's covering the whole bottom of the screen you may run into an issue where only the left part has this delay.
In order to normalize feedback time for the whole button one might use the following solution
(an improved version of Aaron Shekey's):
NSDate *touchDownTime;
- (void)touchDown
{
self.alpha = 0.7;
touchDownTime = [NSDate date];
}
- (void)touchUpInside
{
// basically at least 80ms feedback is guaranteed this way
// note: timeIntervalSinceNow returns negative
NSTimeInterval feedbackTimeLeftToShow =
MAX(0.08 + [touchDownTime timeIntervalSinceNow], 0.001);
[self performSelector:#selector(touchUpInsideAfterFeedback)
withObject:nil
afterDelay:feedbackTimeLeftToShow];
}
- (void)touchUpInsideAfterFeedback
{
self.alpha = 1;
}
note: performSelector may do well with negative delay values, but better be safe than sorry

Hiding and revealing UIButtons

Hi all I am developing a app were in that if we press the UIButton it will show the hiding buttons and once we press the same Button hiding buttons will hide again!!!
I successfully made the hiding buttons to reveal by using the following code:`
mapping1.hidden=NO;
mapping2.hidden=NO;
mapping3.hidden=NO;
but now i want to hide this buttons again by using the same button, how can i do that and if there any other possible way?
To make them hidden again, use
mapping1.hidden = YES;
if (mapping1.hidden)
{
[mapping1 setHidden:NO];
[mapping2 setHidden:NO];
[mapping3 setHidden:NO];
}
else {
[mapping1 setHidden:YES];
[mapping2 setHidden:YES];
[mapping3 setHidden:YES];
}

Resources