iphone - 用于更改 View 的 UISegmented View

标签 iphone ios xcode uiview uisegmentedcontrol

我设置了两个栏按钮项目,一个在右侧,一个在左侧。右边的一个成功地在两个 View 之间切换,右边的第二个是一个分段控件,应该同样在两个 View 之间切换。这是我的代码,我想知道如何使用分段控件实现更改 View 。

- (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;
    }];
}

如何实现分段控件来更改 View ?

最佳答案

用于在 UINavigationBar 上添加 UISegmentedControl

编辑:

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
}

关于iphone - 用于更改 View 的 UISegmented View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14774787/

相关文章:

iphone - 如何将 UIImagePickerController 推送到 UINavigationController 中?

html - 如果屏幕缩放,则无法在 iphone Safari 中滚动 Bootstrap 模式

iphone - 在 iOS 应用程序中播放多种声音(mp3、wav 等)

ios - uitableview 单元格在 ios 7 中自动调整大小。不止一个标签 [IMG]

iphone - 将 UIButton 数组添加到 navigationItem.rightBarButtonItem 会导致 NSInvalidArgumentException

ios - 虽然从一个 Storyboard解雇到另一个 Storyboard,但最近它很快就被解雇了

iphone - UIAlertView 单击时崩溃 - 使用 ARC

android - 如何从手机通讯录中查找注册用户

ios - 纵向模式下的尴尬横向键盘,IOS 8

xcode - 代码签名无效