[英]How to transition between any two ViewControllers in Cocoa Touch?
I'm fairly new iOS development. 我是相当新的iOS开发。 I have multiple
viewController
s for my app, for example - 我的应用有多个
viewController
,例如-
Each of these screens has its own viewController
. 每个屏幕都有其自己的
viewController
。
What's the best way to transition between any two arbitrary viewController
s with an animation? 在带有动画的任意两个任意
viewController
之间过渡的最佳方法是什么?
My current approach is - 我目前的做法是-
viewController
in AppDelegate.m
AppDelegate.m
保留对每个viewController
的引用 This is both cumbersome and seems pretty inefficient, plus I'm not quite sure how to incorprate animated transitions here. 这既麻烦又效率低下,而且我不太确定如何在此处合并动画过渡。
I see some examples with UINavigationController
, but it seems like that operates as a "stack" of views that you can go into and then back out of. 我看到了一些带有
UINavigationController
示例,但似乎将其作为视图的“堆栈”运行,您可以进入然后退出。 I'm not looking to keep a history here, just switch from any view to another. 我不想在这里保留历史记录,只是从任何视图切换到另一个视图。
Any good ways to accomplish this? 有什么好的方法可以做到这一点吗?
Thanks! 谢谢!
Try this Code. 试试这个代码。
AppDelegate.h AppDelegate.h
#import <UIKit/UIKit.h>
#import "LoginViewController.h"
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (strong,nonatomic) UINavigationController *navigationController;
@property (strong,nonatomic) LoginViewController *loginVC;
@end
AppDelegate.m AppDelegate.m
#import "AppDelegate.h"
#import "LoginViewController.h"
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.loginVC = [[LoginViewController alloc]initWithNibName:nil bundle:nil];
self.loginVC.title = @"Login Page";
self.navigationController = [[UINavigationController alloc]initWithRootViewController:self.loginVC];
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
}
LoginViewController.h LoginViewController.h
#import <UIKit/UIKit.h>
#import "MyProfileViewController.h"
@interface LoginViewController : UIViewController
@property (strong,nonatomic)MyProfileViewController *myProfileVC;
@end
Login button action in LoginViewController.m file LoginViewController.m文件中的“登录”按钮操作
- (IBAction)pushMyProfileView:(id)sender
{
self.myProfileVC = [[MyProfileViewController alloc]initWithNibName:nil bundle:nil];
[self.navigationController pushViewController:self.myProfileVC animated:YES];
}
Basically you can achieve that by using: 基本上,您可以使用以下方法实现此目的:
Containment API is probably what you are looking for, you create a UIViewController that is responsible to manage the presentation of its child view controllers. 包含API可能正是您所需要的,您创建一个UIViewController来管理其子视图控制器的表示形式。 Here is Apple Documentation , there are plenty of examples inside.
这是Apple文档 ,里面有很多示例。
As a beginner I believe the easiest solution for you it to use a Storyboard
and create a segue between your View Controller. 作为一个初学者,我相信最简单的解决方案是使用
Storyboard
并在View Controller之间创建序列。
Inside your ViewController you should override prepareForSegue:
method to pass data to the segue.destinationViewController
在ViewController内部,您应该重写
prepareForSegue:
方法以将数据传递给segue.destinationViewController
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.