2

我已经设置了两个 barbutton 项目,一个在右侧,一个在左侧。右边的一个在两个视图之间成功切换,右边的第二个是一个分段控件,应该在两个视图之间切换。这是我的代码,我想知道如何使用分段控件实现更改视图。

- (void)setupNavigationBar {
    UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0,  self.view.frame.size.width, 44.0f)];
    UINavigationItem *navItem = [[UINavigationItem alloc] initWithTitle:@"ContainerView"];
    navItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFastForward target:self action:@selector(switchViewControllers)];
    navBar.items = [NSArray arrayWithObject:navItem];
    segmented = [[UISegmentedControl alloc] initWithItems:
                                  [NSArray arrayWithObjects:
                                   [UIImage imageNamed:@"seg1.png"],
                                   [UIImage imageNamed:@"seg1.png"],
                                   nil]];
    segmented.frame = CGRectMake(0, 0, 50, 30);

    segmented.segmentedControlStyle = UISegmentedControlStyleBar;

    UIBarButtonItem * segmentedBarItem = [[UIBarButtonItem alloc]  initWithCustomView:segmented];
    navItem.leftBarButtonItem = segmentedBarItem;

    [self.view addSubview:navBar];
}

- (void)viewDidLoad {
    [super viewDidLoad];

    // This is just for the purpose of calling the switch method.
    [self setupNavigationBar];

    CGRect rect = CGRectMake(0, 44, 320, 436);

    currentViewController = [[FirstViewController alloc] init];
    currentViewController.view.frame = rect;

    nextViewController = [[SecondViewController alloc] init];
    nextViewController.view.frame = rect;

    [self addChildViewController:currentViewController];
    [self addChildViewController:nextViewController];

    [self.view addSubview:currentViewController.view];
}

- (void)switchViewControllers {
    [self transitionFromViewController:currentViewController toViewController:nextViewController duration:3.0 options:UIViewAnimationTransitionFlipFromRight | UIViewAnimationOptionCurveEaseInOut animations:nil completion:^(BOOL finished) {
        UIViewController *tempViewController = currentViewController;
        currentViewController = nextViewController;
        nextViewController = tempViewController;
        tempViewController = nil;
    }];
}

我将如何实现分段控件来更改视图?

4

2 回答 2

1

UISegmentedControl对于添加UINavigationBar

编辑 :

NSArray *itemArray = [NSArray arrayWithObjects:
                      [UIImage imageNamed:@"1.png"],
                      [UIImage imageNamed:@"2.png"],
                      nil];

UISegmentedControl* menuSegmentedButton = [[UISegmentedControl alloc] initWithItems:itemArray];
menuSegmentedButton.momentary = YES;
menuSegmentedButton.selected = NO;
menuSegmentedButton.segmentedControlStyle = UISegmentedControlStyleBar;
   [menuSegmentedButton addTarget:self action:@selector(doMenu)
              forControlEvents:UIControlEventValueChanged];
[menuSegmentedButton sizeToFit];
UIBarButtonItem* barButton = [[UIBarButtonItem alloc]
                              initWithCustomView:menuSegmentedButton];
self.navigationItem.leftBarButtonItem = barButton;

为点击的分段按钮添加此方法

-(void)doMenu
{
    /// code for segment button tapped
}
于 2013-02-08T14:42:53.010 回答
1

我猜性能问题可能需要使用单独的视图控制器而不是两个子视图,但它要复杂得多——您将拥有一个非常复杂的导航系统,并且需要为每个视图设置段控制。我确信还有另一种方法可以对视图进行子类化,但我对此并不熟悉,因此建议使用一个包含您的视图的视图控制器。

注意:如果您要以编程方式在每个视图中创建所有对象,则可以通过从任何隐藏视图中删除对象来获得出色的性能。

所以首先你有一个 UISegmentedControl (在 iPatel 的回答中创建)。但是在这一行中添加[ctrl addTarget:self action:@selector(switchViewControllers) forControlEvents:UIControlEventValueChanged];

然后输入switchViewControllers

if (ctrl.selectedSegmentIndex == 0) {
    NSLog(@"One");
    self.viewOne.hidden = NO;
    self.viewTwo.hidden = YES;

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:@"showView" forKey:@"viewOne"];
    [defaults synchronize];
}
else if (ctrl.selectedSegmentIndex == 1) {
    NSLog(@"Two");
}

-(void)viewDidLoad {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    if ([[defaults objectForKey:@"showView"] isEqualToString:@"viewOne"]) {
        self.viewOne.hidden = NO;
        self.viewTwo.hidden = YES;
    }
    else if (...) {

    }
}

当您切换段控制时,上述内容应该有效。在每个if中,您都可以切换两个视图的可见性。您将创建不同的视图作为主视图的子视图;一个在另一个之上。您需要在 Interface Builder 或 viewDidLoad 中隐藏一个视图(我建议将当前视图存储在 NSUserDefaults 中,并在用户第一次加载视图控制器时显示该视图)。

于 2013-02-08T14:49:45.237 回答