1

每个 selectedSegmentIndex 添加不同的视图控制器。

在第一次segmentindex 点击,呈现新的视图控制器和第二段索引,呈现另一个视图控制器。

和段控制只显示底部。(不在导航栏中添加)。

段控制,因为它是可见的每个视图控制器(如标签栏控制器)。

我的问题是当新控制器弹出段控制器禁用时。需要帮助才能完成这项工作。

4

1 回答 1

0

在 .h 文件中尝试此代码添加

进口

@interface SegmentManagingViewController : UIViewController <UINavigationControllerDelegate> {
    UISegmentedControl    * segmentedControl;
    UIViewController      * activeViewController;
    NSArray               * segmentedViewControllers;
   IBOutlet UILabel *theLabel;
    IBOutlet UIImageView *image;
   }
//-(void)setTextColorsForSegmentedControl:(UISegmentedControl*)segmented;
@property (nonatomic, retain, readonly) IBOutlet UISegmentedControl * segmentedControl;
@property (nonatomic, retain, readonly) UIViewController * activeViewController;
@property (nonatomic, retain, readonly) NSArray * segmentedViewControllers;

@end

在 .m 文件中添加

 #import "SegmentManagingViewController.h"
     #import "CategoryViewController.h"
     #import "AtoZViewController.h"

    @interface SegmentManagingViewController ()

    @property (nonatomic, retain, readwrite) IBOutlet UISegmentedControl * segmentedControl;
    @property (nonatomic, retain, readwrite) UIViewController            * activeViewController;
    @property (nonatomic, retain, readwrite) NSArray                     * segmentedViewControllers;
    - (void)didChangeSegmentControl:(UISegmentedControl *)control;
    - (NSArray *)segmentedViewControllerContent;

    @end

    @implementation SegmentManagingViewController

    @synthesize segmentedControl, activeViewController, segmentedViewControllers;

    - (void)viewDidLoad {
        [super viewDidLoad];
        self.navigationItem.title =@"Breadworks";
        //self.navigationItem.
        [image release];
        theLabel = [[UILabel alloc] initWithFrame:CGRectMake(140,-40,180,60)];
            theLabel.backgroundColor = [UIColor colorWithRed:120.0f/255.0f green:69.0f/255.0f blue:53.0f/255.0f alpha:1.0f ];

        [theLabel setFont:[UIFont fontWithName:@"Noteworthy " size:15]];
        //[self.view addSubview:theLabel];
        self.segmentedViewControllers = [self segmentedViewControllerContent];
        //self.navigationController.navigationBarHidden=YES;
        NSArray * segmentTitles = [self.segmentedViewControllers arrayByPerformingSelector:@selector(title)];
        self.segmentedControl = [[UISegmentedControl alloc] initWithItems:segmentTitles];
        self.segmentedControl.tintColor = [UIColor colorWithRed:255.0f/255.0f green:252.0f/255.0f blue:235.0f/255.0f alpha:1.0f];
        self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:120.0f/255.0f green:69.0f/255.0f blue:53.0f/255.0f alpha:1.0f ];
        self.segmentedControl.frame=CGRectMake(0,0,768, 73);
        self.segmentedControl.selectedSegmentIndex = 0;
        self.segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
        //self.navigationController.navigationBar.frame = CGRectMake(0, 0,760, 100);
        [self.segmentedControl addTarget:self
                                  action:@selector(didChangeSegmentControl:)
                        forControlEvents:UIControlEventValueChanged];
        self.view.frame = CGRectMake(0,4,330, 53);
        self.view.backgroundColor = [UIColor colorWithRed:255.0f/255.0f green:252.0f/255.0f blue:235.0f/255.0f alpha:1.0f];

       [segmentedControl setTitle:@"1" forSegmentAtIndex:0]
           [segmentedControl setTitle:@"2" forSegmentAtIndex:1];
           [self.view addSubview:segmentedControl];
        [self.segmentedControl release];

        [self didChangeSegmentControl:self.segmentedControl]; // kick everything off


    }

    - (NSArray *)segmentedViewControllerContent {

        UIViewController * controller1 = [[CategoryViewController alloc] initWithParentViewController:self];
        UIViewController * controller2 =[[AtoZViewController alloc] initWithParentViewController:self] ;
        NSArray * controllers = [NSArray arrayWithObjects:controller1, controller2, nil];

        [controller1 release];
        [controller2 release];

        return controllers;
    }

    #pragma mark -
    #pragma mark Segment control

    - (void)didChangeSegmentControl:(UISegmentedControl *)control {
        if (self.activeViewController) {
            [self.activeViewController viewWillDisappear:NO];
            [self.activeViewController.view removeFromSuperview];
            [self.activeViewController viewDidDisappear:NO];
        }
        //self.segmentedControl.frame=CGRectMake(5,4,300, 50);


        self.activeViewController = [self.segmentedViewControllers objectAtIndex:control.selectedSegmentIndex];

        [self.activeViewController viewWillAppear:NO];
        [self.view addSubview:self.activeViewController.view];
        [self.activeViewController viewDidAppear:NO];

        NSString * segmentTitle = [control titleForSegmentAtIndex:control.selectedSegmentIndex];
        self.navigationItem.backBarButtonItem  = [[UIBarButtonItem alloc] initWithTitle:segmentTitle style:UIBarButtonItemStylePlain target:nil action:nil];
        /*if(self.segmentedControl.selectedSegmentIndex == 0)
         {
         segmentedControl.tintColor = [UIColor colorWithRed:255.0f/255.0f green:252.0f/255.0f blue:235.0f/255.0f alpha:10.0f];
         }
         else
         {

         //self.segmentedControl.tintColor = [UIColor redColor];
         }*/
    }

    #pragma mark -
    #pragma mark View life cycle

    - (void)viewWillAppear:(BOOL)animated {
        [super viewWillAppear:animated];
        [self.activeViewController viewWillAppear:animated];
    }

    - (void)viewDidAppear:(BOOL)animated {
        [super viewDidAppear:animated];
        [self.activeViewController viewDidAppear:animated];
    }

    - (void)viewWillDisappear:(BOOL)animated {
        [super viewWillDisappear:animated];
        [self.activeViewController viewWillDisappear:animated];
    }

    - (void)viewDidDisappear:(BOOL)animated {
        [super viewDidDisappear:animated];
        [self.activeViewController viewDidDisappear:animated];
    }



    #pragma mark -
    #pragma mark Memory management

    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];

        for (UIViewController * viewController in self.segmentedViewControllers) {
            [viewController didReceiveMemoryWarning];
        }
    }

    - (void)viewDidUnload {
        self.segmentedControl         = nil;
        self.segmentedViewControllers = nil;
        self.activeViewController     = nil;
        //[self.segmentedControl release];
        [super viewDidUnload];

    }

    @end
于 2012-12-14T10:15:24.397 回答