简体   繁体   English

使用分段控件在2个视图之间切换

[英]Use segmented Control to switch between 2 views

I make an iphone application that use a segmented Control to switch between 2 viewControllers that display different informations. 我制作了一个iphone应用程序,它使用分段控件在两个显示不同信息的viewController之间切换。 So, I defined in the first view Controller a segmented Control that I linked in IB to a Segmented Control that I place on the corresponding view. 因此,我在第一个视图控制器中定义了一个分段控件,我在IB中将其链接到我放置在相应视图上的分段控件。

@interface FirstViewController : UIViewController{

//NSArray                * viewControllers;
//UINavigationController * navigationController;
IBOutlet UISegmentedControl *segment; //->segment linked in the nib of FirstViewController

}

The action related to the segmented control is the following: 与分段控件相关的操作如下:

-(IBAction)valuechanged:(id)sender{

NSInteger index = [(UISegmentedControl *)sender selectedSegmentIndex];

UIViewController *parking=[[ParkingViewController alloc] 
initWithNibName:@"ParkingViewController" bundle:nil] ;

viewControllers= [NSArray arrayWithObjects:self,parking,nil];


if(index==1){
    UIViewController * incomingViewController = [viewControllers objectAtIndex:index];
    [self presentModalViewController:incomingViewController animated:YES];
            }   
}

In this Action, I define what to do once user click on segmented control.Here, it's loading a new viewController named ParkingViewController. 在这个Action中,我定义了一旦用户点击分段控件后该做什么。这里,它正在加载一个名为ParkingViewController的新viewController。 The problem is that once the new ParkingViewController is loaded the segmentedControl disappear and so I can't come back to the firstViewController. 问题是,一旦加载了新的ParkingViewController,segmentedControl就会消失,所以我无法回到firstViewController。

I don't know how to do to keep the segmentedControl for both views? 我不知道如何保持两个视图的segmentedControl?

Thank u all 谢谢大家

Quentin 昆汀

Generally, the UITabBarController is used to control switching between two or more view controllers. 通常,UITabBarController用于控制两个或多个视图控制器之间的切换。 It will stay on the screen and allow switching back and forth. 它将保留在屏幕上并允许来回切换。

The segmented controller you have cannot stay on the screen when you present a modal view. 当您呈现模态视图时,您拥有的分段控制器无法保留在屏幕上。 A modal view will take over the entire screen. 模态视图将占据整个屏幕。 Also, because you are animating it onto the screen, a copy of the segmented view controller in the new view will not appear to be the same segmented control because the user saw it scroll onto the screen. 此外,由于您将其设置为屏幕动画,因此新视图中的分段视图控制器的副本将不会显示为相同的分段控件,因为用户看到它滚动到屏幕上。

I can't come back to the firstViewController... ?? 我不能回到firstViewController ......?

You can add a UIButton and call an IBAction [self dismissModalViewControllerAnimated:NO]; 你可以添加一个UIButton并调用一个IBAction [self dismissModalViewControllerAnimated:NO];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM