我在右侧设置了两个按钮项目,在左侧设置了一个按钮项目。正确的一个在两个视图之间成功地改变,而右边的第二个是一个分段控件,它应该在两个视图之间改变,就像明智一样。这是我的代码,我想知道如何使用分段控件实现更改视图。
- (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;
}];
}
如何实现分段控件来更改视图?
答案 0 :(得分: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
}
答案 1 :(得分:1)
我认为性能问题可能需要单独的视图控制器而不是两个子视图,但它要复杂得多 - 你会有一个非常复杂的导航系统,需要为每个视图设置段控制。我确信有另一种方法可以对视图进行子类化,但我对此不熟悉,因此建议使用一个包含视图的视图控制器。
注意:如果您要以编程方式在每个视图中创建所有对象,则可以通过从任何隐藏视图中删除对象来获得出色的性能。
首先你有一个UISegmentedControl(在iPatel的回答中创建)。但请添加此行[ctrl addTarget:self action:@selector(switchViewControllers) forControlEvents:UIControlEventValueChanged];
然后在switchViewControllers
put:
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中并在用户首次加载视图控制器时显示该视图)。