Kutay Demireren
Kutay Demireren

Reputation: 650

Switching Between Views with a UISegmentedControl

As it is clear from the title, I'm trying to find a way to switch between views using segmented control. Actually, I got something already and it is working. You can find it below:

- (IBAction)segmentChanged:(id)sender{
    UISegmentedControl *segmentedControl = sender;
    switch (segmentedControl.selectedSegmentIndex) {
        case 0:
            self.currencyContainer.hidden = NO;
            self.goldContainer.hidden = YES;
            self.alarmContainer.hidden = YES;
            break;
        case 1:
            self.currencyContainer.hidden = YES;
            self.goldContainer.hidden = NO;
            self.alarmContainer.hidden = YES;
            break;
        case 2:
            self.currencyContainer.hidden = YES;
            self.goldContainer.hidden = YES;
            self.alarmContainer.hidden = NO;
            break;
        default:
            break;
    }
}

So, it depends on the principle becoming hidden and visible. I think it is so common to use, because generally people had the solutions in this way, as I searched from the internet. However, I want it to be slightly different. With my code, all the views are loaded at once, but I want to load when just the views come on the screen. If I switch from 0 to 1 index segment, then I want to load 1st view and show on the screen.

How can I manage it?

Thanks.

Upvotes: 1

Views: 683

Answers (2)

App Dev Guy
App Dev Guy

Reputation: 5536

I have answered a similar (not the same, but similar result) question here: How To Switch Views With NSNotifications

Basically you can switch views using NSNotifications. Your fist view will load and using NSNotifications you can send a notification to a custom class that listens and responds by changing the view; when you select an index on the UISegmentControl, your views would change.

It would look like this in your code, in your NSObject Class ButtonHandler:

- (IBAction)segmentChanged:(id)sender{
    UISegmentedControl *segmentedControl = sender;
    switch (segmentedControl.selectedSegmentIndex) {
        case 0:
            [[NSNotificationCenter defaultCenter] postNotificationName:@"notifyButtonPressed1" object:self];
        break;
        case 1:
            [[NSNotificationCenter defaultCenter] postNotificationName:@"notifyButtonPressed2" object:self];
        break;
        case 2:
            [[NSNotificationCenter defaultCenter] postNotificationName:@"notifyButtonPressed3" object:self];
        break;
        default:
        break;
    }
}

Then simply follow the rest of the code I have set out in the other post.

This is the effect:

Switch Views with NSNotifications

UPDATE

I have created a tutorial on how to do this at this link: iOS Custom Navigation with UISegmentedControl

To set the view to be the same as the device bound use the following:

//this will get the size of your device view 
//and set it to width and height aka w & h
int w = self.view.frame.size.width;
int h = self.view.frame.size.height;

//THIS SETS THE SIZE AND POSITION OF THE NEW CONTENT
self.content.view.frame = CGRectMake(0, 0, w, h);

Upvotes: 1

Bhushan B
Bhushan B

Reputation: 2510

You can achieve this by using Lazy Stored Properties something like this :

lazy var currencyContainer:UIView = {
     var newCurrencyContainer = UIView()
     // your initialization code
     return newCurrencyContainer
}()

A lazy stored property is a property whose initial value is not calculated until the first time it is used. - Apple Docs

OR

By using optional variable like this :

var goldContainer:UIView?

and later checking

if let goldContainer = goldContainer {
     // view is already created
} else {
     // your custom init
     // goldContainer = UIView()
}

Hope this helps you.

Upvotes: 1

Related Questions