Val Opfermann
Val Opfermann

Reputation: 107

Using UISegmentedControl to show/hide buttons

How can I use UISegmentedControl in Objective C programming to show or hide some buttons that are on my screen?

Another question on this site showed this code:

if (selectedSegment == 0) {
    [firstView setHidden:NO];
    [secondView setHidden:YES];
} else {
    [firstView setHidden:YES];
    [secondView setHidden:NO];
}

But how exactly do I put something into firstView and secondView? Please add a UIButton as an example if anyone shows me an example. Note: I canNOT use a View Based application to do this, due to the fact that my program is pretty far along. Thanks in advance.

Upvotes: 0

Views: 548

Answers (1)

james00794
james00794

Reputation: 1147

After your @implementation line in the view controller:

UIButton *firstButton;
UIButton *secondButton;

In your view controller, in the viewDidLoad function (or wherever you want to initialize your buttons), initialize your buttons like so:

firstButton = [UIButton buttonWithType:(UIButtonTypeRoundedRect)];
[firstButton setFrame:CGRectMake(20, 100, 50, 50)];
secondButton = [UIButton buttonWithType:(UIButtonTypeRoundedRect)];
[secondButton setFrame:CGRectMake(20, 150, 50, 50)];

Obviously, change the style to your choosing and use CGRectMake to position the buttons somewhere on your screen. Then when you want to hide/show a button:

if (selectedSegment == 0) {
    [firstButton setHidden:NO];
    [secondButton setHidden:YES];
} else {
    [firstButton setHidden:YES];
    [secondButton setHidden:NO];
}

Upvotes: 1

Related Questions