简体   繁体   English

在3个或更多视图之间切换

[英]Switch between 3 or more views

Im new to iPhone development and I have really taken this to me. 对iPhone开发来说,我是新手,我真的很喜欢这个。 I love it, but there is one thing thats naggin' me. 我喜欢它,但有一件事困扰着我。 How do I keep switching view? 如何保持切换视图? I know how to come from first view that is given to me when I create a new project, to a view that I make, but how do I get passed this two windows? 我知道如何从创建新项目时给我的第一个视图到我创建的视图,但是如何通过这两个窗口? How do I get from to views that I created? 我如何进入创建的视图?

I have this app which have a main window with a NavigationController whih is feed with a UITableViewController. 我有这个应用程序,它有一个带有NavigationController的主窗口,其中带有UITableViewController。 This is my main menu. 这是我的主菜单。 I have a in the upper right corner, a "+"-button which gives me a new view, but how do I get a new view from here? 我在右上角有一个“ +”按钮,它为我提供了新视图,但是如何从此处获得新视图? How do I push a new view when the user pick something to add? 用户选择要添加的内容时如何推送新视图?

Hope someone understand my question. 希望有人理解我的问题。 A link to some documentation would be fine. 链接到一些文档会很好。 I have looked everywhere. 我到处都看过。

You can do this many diffrent way, you can do what the Sebastian said, you can also have a common RootViewController that manages your other view controllers view. 您可以采用多种不同的方式,可以执行Sebastian所说的,也可以使用通用的RootViewController来管理其他视图控制器视图。 This is what I like to do, I actually define a protocol on the RootViewController something like ToggleView:UIViewController newController UIViewController:oldController. 这就是我想做的,实际上我在RootViewController上定义了一个协议,例如ToggleView:UIViewController newController UIViewController:oldController。 I make any UIViewController that i want to be able to switch from that view to another implement this protocol. 我制作了所有希望能够从该视图切换到另一个实现该协议的UIViewController。 This makes since because generally when you click on a button, you know what View you want to go to next. 这样做是因为通常在单击按钮时,您知道要转到下一个视图。 So when a user clicks the button, in the UIViewController that owns the button i create the new ViewController whose view i want pushed into the screen, this is nice because it also allows me to set up data in the view controller and not have to delegate it to some other object or use a singleton to get the data in the new view, then i call my toggleView methods and the root view controller does the switching. 因此,当用户单击按钮时,在拥有按钮的UIViewController中,我创建了一个新的ViewController,希望将其视图推入屏幕,这很好,因为它还允许我在视图控制器中设置数据,而不必委托它到其他对象或使用单例在新视图中获取数据,然后调用我的toggleView方法,然后由根视图控制器进行切换。 I find this works pretty well and theres berely any code involved. 我发现这很好用,几乎没有涉及任何代码。 I dont always u se this though, if I know a new view will always come out of another particular view, (for example a home page where one views events and creation of those events), in this case I will loosly couple the view controllers by using protocols. 但是,我并不总是这样,如果我知道一个新视图总是会从另一个特定视图中产生(例如,一个主页,其中一个视图可以查看事件和这些事件的创建),在这种情况下,我会松散地耦合视图控制器通过使用协议。

For that particular situation, people usually use the presentModalViewController:animated: method of UIViewController . 对于这种特殊情况,人们通常使用UIViewControllerpresentModalViewController:animated:方法。 UINavigationController is a subclass of UIViewController , so your code would look something like this: UINavigationControllerUIViewController的子类,因此您的代码如下所示:

UIViewController *addingViewController = [[UIViewController alloc] initWithNibName:@"AddingView" bundle:nil];
[[self navigationController] presentModalViewController:addingViewController animated:YES];
[addingViewController release];

Here is the rootviewcontrollerdelegate definition 这是rootviewcontrollerdelegate定义

@protocol RootViewControllerViewDelegate @protocol RootViewControllerViewDelegate

-(void)toggleView:(UIViewController )newController viewController:(UIViewController )oldController; -(void)toggleView:(UIViewController )newController viewController:(UIViewController )oldController;

@end @结束

a possible implementation to toggleView 可能的实现toggleView

-(void)toggleView:(UIViewController *)newController viewController:(UIViewController*)oldController {   
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1];
[UIView setAnimationTransition:([oldController.view superview] ?  UIViewAnimationTransitionFlipFromLeft : UIViewAnimationTransitionFlipFromLeft) forView:self.view cache:YES];
[newController viewWillAppear:YES];
[oldController viewWillDisappear:YES];
[oldController.view removeFromSuperview];
[self.view addSubview:newController.view];
[oldController viewDidDisappear:YES];
[newController viewDidAppear:YES];
[UIView commitAnimations];
[oldController release];

} }

This will swipe the view controllers by flipping the view 这将通过翻转视图来滑动视图控制器

Obviously you must make a new RootViewController somewhere and start with a view there, (could be the app delegate) 显然,您必须在某个地方创建一个新的RootViewController并从那里开始一个视图(可能是应用程序委托)

Now if you want a ViewController to be able to use the RootViewController it must conform to the protocol, you declare it in that classes interface like so 现在,如果您希望ViewController能够使用RootViewController,它必须符合协议,则可以在该类接口中声明它,如下所示:

@interface MyViewController : UIViewController <RootViewControllerDelegate> {
id delegate;
}@property(assign) id <RootViewControllerViewDelegate> delegate;

Now you can use the delegates method to swap a view for another given that everything has been initialized right. 现在,假设一切都已正确初始化,则可以使用委托方法将视图交换为另一个视图。 the code to swap two controllers view could look like this 交换两个控制器视图的代码如下所示

    NewViewController *viewController=...
    //you can set up your viewControllers data here if you need to
   //Since its probable that this view has that data it can just set it instead of
   //delegating 
viewController.delegate=delegate; //setting up the RootViewController reference
[delegate toggleView:viewController viewController:self]; 

remember on the toggleView call back to release the old ViewController, if you dont youll get a leak since you lose all reference to that controller. 请记住在toggleView回调上释放旧的ViewController,如果不这样做,则会泄漏,因为您丢失了对该控制器的所有引用。

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

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