开发者

How to make UISegmentedControl act like a UITabBarController?

How can I use UISegmentedControl to load differen开发者_JAVA百科t subviews when different segments are selected? Im new to objective-c and iOS programming.

OR is there a way to make UITabBarController look like a UISegmentedControl?


For a programatic approach

in loadView:

{
    NSArray *segments = [NSArray arrayWithObjects:@"Left", @"Right", nil];


    segmentedControl = [[UISegmentedControl alloc]initWithItems:segments];
    [segmentedControl addTarget:self
    action:@selector(changeSubViews)
    forControlEvents:UIControlEventValueChanged];
    contentView = [UIView alloc]initwithFrame:(the frame where you want the subViews to be displayed)];
    [self.view addSubView:contentView];

    }

    - (void)changeSubViews
        {
            switch(segmentedControl.selectedSegmentIndex)
            {
            case 0:
                {
                    [rightView removeFromSuperView];
                    if (leftView ==nil){leftView alloc, init;}
                    [contentView addSubView:leftView];
                    break;
                }
            case 1:
                {
                    [leftView removeFromSuperView];
                    if (rightView ==nil){rightView alloc, init;}
                    [contentView addSubView:rightView];
                    break;
                }
            }
    }


You could add a UIToolbar to your root controller's view. In it, you'd have a UISegementedControl with actions that the root controller handle. Depending on the segment clicked, you would load up a different view and display the view under the UIToolbar (and anything else that you want the view to be below).

Hope this helps!


You should consider crafterm's answer in this post: UISegmentedControl Best Practice

This will allow you to maintain your normal ViewController behavior (support rotation, memory warnings, etc.) while allowing for the segmented control on top of it.


Ok for this purpose you make two views in your view and make property for both in .h file and Attach an IBAction to the segmented control and write code like this

if(self.yourSegmentedControl.selectedSegmentIndex==0)
    {   
    view1.hidden=YES;
        view2.hidden=NO;
    }
    else if(self.categorySegmentedControl.selectedSegmentIndex==1)
    {  
    view2.hidden=YES;
        view2.hidden=NO:
    }

Hope this will help you.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜