简体   繁体   English

如何在Cocoa Touch中的任何两个ViewController之间过渡?

[英]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 ,例如-

  • A login screen 登录屏幕
  • A main / my profile screen 主/我的个人资料屏幕
  • A leader board / standings screen 排行榜/排名榜
  • etc.. 等等..

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 - 我目前的做法是-

  1. Keep a reference to each viewController in AppDelegate.m AppDelegate.m保留对每个viewController的引用
  2. Keep changing the window's root controller as needed 继续根据需要更改窗口的根控制器

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: 基本上,您可以使用以下方法实现此目的:

  1. Apple containers (UINavigationController, UITabbarController, UISplitController) 苹果容器(UINavigationController,UITabbarController,UISplitController)
  2. Containment API 遏制API
  3. Using "normal" view controller and modal presentation 使用“普通”视图控制器和模式表示

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.

相关问题 如何使用 iOS swift hero 框架在两个视图控制器之间进行转换? - How to use iOS swift hero framework for transition between two viewcontrollers? 由rootView控制器呈现的两个viewController之间的过渡 - Transition between two viewControllers presented by rootView controller 将对viewControllers的引用传递给其他viewControllers可可触摸 - Passing references to viewControllers to other viewControllers cocoa touch 如何添加过渡以在选项卡视图控制器之间进行切换 - How to add transition for switching between tabbar viewcontrollers 如何在iOS中的两个自定义可可触摸框架之间共享类 - How to share classes between two custom cocoa touch frameworks in iOS ViewControllers之间的转换不起作用 - transition between ViewControllers not working ViewController之间的过渡不起作用 - Transition between ViewControllers is not working (iOS)如何在两个ViewController之间的过渡中为UI元素(例如标签和图像)的过渡设置动画? - (iOS) How can i animate the traslation of UI elements, like labels and images in the transition between two viewcontrollers? 在过渡动画期间在两个viewController之间共享图像 - Sharing an Image between two viewControllers during a transition animation ViewController之间的滞后自定义过渡 - Laggy Custom Transition between ViewControllers
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM