开发者

Changing Views from UISegmentedControl

I have a best practice type of question regarding Nibs and UISegmentedControls.

I have a nib file which contains a UISegmentedControl with 3 buttons. Each of which displays a different view in the main part of the window when toggled.

Everything is working fine but I suspect my approach is flawed and was wondering if someone could suggest the way I was "meant" to do it.

Currently in the NIB I have 3 UIViews which are overlayed on top of one another and then in the .m file I modify each UIViews .hidden to hide and reveal the one I am interested in.

- (IBAction)segmentAction:(id)sender
{
    if([sender selectedSegmentIndex] == 0)
    {
        [self.view1 setHidden:NO];
        [self.view2 setHidden:YES];
      开发者_如何学Python  [self.view3 setHidden:YES];
    }
    else if([sender selectedSegmentIndex] == 1)
    {
        [self.view1 setHidden:YES];
        [self.view2 setHidden:NO];
        [self.view3 setHidden:YES];
    }
    else if([sender selectedSegmentIndex] == 2)
    {
        [self.view1 setHidden:YES];
        [self.view2 setHidden:YES];
        [self.view3 setHidden:NO];
    }
}

Everything "works" but the nib file is a pain to edit as you can't easily "turn off" the other UIViews so I invariably select the wrong element on the canvas. Also IB complains that: "This view overlaps one of its siblings" in the info dialog.

What is the approach I should be taking given that I would like all of the views to be laid out in InterfaceBuilder. Should I have 4 nib files? If so how should I load them in when a segmentControl is pressed? Any sample code you can direct me to?

thanks


off the top of my head, here are a couple things you can try:

1) make 3 separate UIView objects in IB, but don't add them to the view heirarchy

Assuming that you have a view controller where all three views are contained, you can, in IB, create the 3 views by dragging each view from the library to the window titled YourNib.nib or YourXib.xib. So don't drag the views into the view controller's main view.

Now you can double click each one and edit them on their own, without having to try to select the different views because they're overlapped (because they aren't)

in the connections tab, connect the view controller's view property to one of the views.

this sets the default.

now in your code, you can change which view is connected to:

[myViewController setView:view2];

and reload the views.

2) make one really tall view (or really wide view) and change the y-position of the view w/ respect to the parent view's frame when you click a segmented view button so that it appears to user as if they're going to separate pages

this makes it easy to animate and transition between "views". though will use more memory because you have all elements loaded even when they're not visible. but you were doing that anyway.


This will work as you have it, but you're right that it's not ideal. Without any context, the most natural way to implement this would be a UITabBarController managing three UIViewControllers, each with it's own view. You'll get the benefit of all the callbacks as you switch views (viewWill/DidAppear, viewWill/DidDisappear, etc) + it will be easier to separate your nib files. The callbacks can be really helpful for loading/reloading data at the appropriate time.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜