How to set visibility of a group of views in iOS - 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)];

Related

How to remove view on touch?

I have a button hooked up in the storyboard to a method onButtonPress. In that method I call [pressedButton removeFromSuperview] but the view is not removed. I have even tried [_scrollView setNeedsDisplay]; and [_scrollView setNeedsLayout]; with no luck. I am assuming this is a restriction on being able to remove the button I have pressed. Is there a way I can signal to the view controller to call a method in the future to remove this button?
you can just hide it,
- (IBAction)celebritiesButtonPressed:(id)sender {
self.button.alpha = 0;
//[self.peopleButton removeFromSuperview]; //as you intend
}
Nevermind, actually. I was making a silly mistake.
The view I was pressing is part of a custom table I implemented to avoid the tableview in the scrollview. What I was doing was removing all the table cells from the table then re-adding them with the exception of the one that was clicked. Except right before I looped through the array which held the views and called [view removeFromSuperview], I removed the view I pressed from that array. Therefore, it was never calling that method.
So I was doing
[_taskViewCells removeObject:t];
for (id taskViewCell in _taskViewsCells) {
[taskViewCell removeFromSuperview];
// Remove all the task views
}
Pretty silly...

collection view with buttons

I have a collection view of labels and now need one with buttons, but it is not quite the same setting it up. I currently call the label collection view like this
[(UILabel *)self.EventTitles[i] setText:[object objectForKey:#"name"]];
which works fine but set text is not an option for UIButton collection views so I need a slight alteration in which I can not figure out. Can someone help me out with this?
I assume that in the case of the UIButtons you want to change their title. To do that you need to use the following method:
- (void)setTitle:(NSString *)title forState:(UIControlState)state
In your specific case of a UIButton collection you can do the following:
[(UIButton *)self.buttonCollection[i] setTitle:#"yourTitle" forState:UIControlStateNormal];

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];

Why can't I set backgroundImage of UIButton after removing all subviews

Just having some hard time with UIButton. I remove all subviews using
for (UIView *v in button.subviews) {
[v removeFromSuperview];
}
But I want to set the background image afterwards using
[button setBackgroundImage:[UIImage imageNamed:#"backgroundImage.png"] forState:UIControlStateNormal];
and nothing happens. If I do not remove all subviews than the previous code works.
So is it possivle that removing all subviews actually removes the backgroundImage as well, in which case, is it possible to put the backgroundImage back in?
Yes, according to what you experienced, the background image seems to be shown using a separate subview. Short answer: instead of raping poor view hierarchy (which is private anyway), you should keep track of the subviews you added yourself and remove only those.

Hide one view and unhide another upon touching a button

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.

Resources