gpt4 book ai didi

iphone - UISegmented View 改变 View

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:16:10 25 4
gpt4 key购买 nike

我已经设置了两个barbutton 项,一个在右边,一个在左边。右边的一个成功地在两个 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 - UISegmented View 改变 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14774787/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com