如何在几个视图控制器之间创建一系列模态/推送segue以保持所有推/模态视图在同一堆栈上?(How to create a sequence of modal/push segues between

编程入门 行业动态 更新时间:2024-10-27 22:33:17
如何在几个视图控制器之间创建一系列模态/推送segue以保持所有推/模态视图在同一堆栈上?(How to create a sequence of modal/push segues between few view controllers to keep all the push/modal views on the same stack? (using nibs))

我正在尝试以正确的方式创建push和modal segues,并且从我的理解是将我的根视图控制器嵌入到我的app委托中的导航控制器中......所以当我想创建segues时,他们都将是模态/推到同一堆栈..是吗?

我真的想要理解这一点,因为我正确地做了一些问题。

我希望能够执行:

FirstViewController >点击按钮(模态segue)> SecondViewController >点击按钮(推)> ThirdViewController

从ThirdViewController我将点击取消获取堆栈的ThirdViewController并进入SecondViewController单击取消获取堆栈的SecondViewController并转到FirstViewController,这是我的rootviewcontroller,它将是viewstack中唯一的视图控制器...

是不是它的工作方式如何? 如果我错了,请纠正我。

到目前为止我是这样做的:(我知道如何解除视图控制器,所以我没有添加此代码)

AppDelegate.h:

#import <UIKit/UIKit.h> #import <CoreData/CoreData.h> #import "FirstViewController" @interface AppDelegate : UIResponder <UIApplicationDelegate> @property (strong, nonatomic) UIWindow *window; @property (strong, nonatomic) UINavigationController *navigationController; @property (strong, nonatomic) FirstViewController *firstViewController; @end

AppDelegate.m:

@implementation AppDelegate @synthesize firstViewController; - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. firstViewController = [[FirstViewController alloc]init]; self.navigationController = [[UINavigationController alloc]initWithRootViewController: firstViewController]; [self.window setRootViewController:self.navigationController]; [self.window makeKeyAndVisible]; return YES; }

FirstViewController.h:

#import <UIKit/UIKit.h> #import "SecondViewController.h" @interface HomeViewController : UIViewController - (IBAction)goToSecond:(id)sender; @end

FirstViewController.m:

@interface FirstViewController () @end @implementation FirstViewController - (id)init { self = [super initWithNibName:@"FirstViewController" bundle:nil]; if (self) { // Do something } return self; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view from its nib. } - (IBAction)goToSecond:(id)sender { SecondViewController *secondViewController = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil]; [self.navigationController presentViewController: secondViewController animated:YES completion:nil]; }

现在在第二个ViewController中,我正在尝试思考如何到达rood导航控制器,这样我就可以添加另一个屏幕,而不是在SecondViewController中创建另一个导航控制器。

I'm trying to create push and modal segues the right way, and from what I understand is to embed my root view controller in a navigation controller in my app delegate...So when I want to create segues they all will be modal/pushed to the same stack..is that right?

I'm really trying to understand this since i'm having some issues doing it correctly.

I want to be able to perform :

FirstViewController > click on a button (modal segue) > SecondViewController > click on a button (push) > ThirdViewController

And from the ThirdViewController I will click on cancel take the ThirdViewController of the stack and get to the SecondViewController click cancel take SecondViewController of the stack and get to the FirstViewController which is my rootviewcontroller and it will be the only view controller in the viewstack...

isn't it how it's suppose to work? please correct me if I'm wrong.

This is how I did it so far: (I know how to dismiss view controllers so I did not add this code)

AppDelegate.h:

#import <UIKit/UIKit.h> #import <CoreData/CoreData.h> #import "FirstViewController" @interface AppDelegate : UIResponder <UIApplicationDelegate> @property (strong, nonatomic) UIWindow *window; @property (strong, nonatomic) UINavigationController *navigationController; @property (strong, nonatomic) FirstViewController *firstViewController; @end

AppDelegate.m:

@implementation AppDelegate @synthesize firstViewController; - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. firstViewController = [[FirstViewController alloc]init]; self.navigationController = [[UINavigationController alloc]initWithRootViewController: firstViewController]; [self.window setRootViewController:self.navigationController]; [self.window makeKeyAndVisible]; return YES; }

FirstViewController.h:

#import <UIKit/UIKit.h> #import "SecondViewController.h" @interface HomeViewController : UIViewController - (IBAction)goToSecond:(id)sender; @end

FirstViewController.m:

@interface FirstViewController () @end @implementation FirstViewController - (id)init { self = [super initWithNibName:@"FirstViewController" bundle:nil]; if (self) { // Do something } return self; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view from its nib. } - (IBAction)goToSecond:(id)sender { SecondViewController *secondViewController = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil]; [self.navigationController presentViewController: secondViewController animated:YES completion:nil]; }

Now in the second ViewController I'm trying to think how to get to the rood navigation controller so I can add to it another screen instead of creating another navigation controller in SecondViewController.

最满意答案

如果您首先想要使用modal segue然后使用push segue,则需要将模态视图控制器嵌入到UINavigationController的实例中。

对于您的设置,它将如下所示:

- (IBAction)goToSecond:(id)sender { SecondViewController *secondViewController = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil]; UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:secondViewController]; [self.navigationController presentViewController:navigationController animated:YES completion:nil]; // can be [self presentViewController:navigationController animated:YES completion:nil] as well... }

然后你可以在SecondViewController实现例如goToThird , SecondViewController所示:

- (IBAction)goToThird { ThirdViewController *thirdViewController = [[ThirdViewController alloc] initWithNibName:@"ThirdViewController" bundle:nil]; [self.navigationController pushViewController:navigationController animated:YES completion:nil]; }

If you first want to use a modal segue and then a push segue, you need to embed the modal view controller into an instance of UINavigationController.

For your setup, it'll look like this:

- (IBAction)goToSecond:(id)sender { SecondViewController *secondViewController = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil]; UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:secondViewController]; [self.navigationController presentViewController:navigationController animated:YES completion:nil]; // can be [self presentViewController:navigationController animated:YES completion:nil] as well... }

Then you can implement e.g. goToThird in SecondViewController like so:

- (IBAction)goToThird { ThirdViewController *thirdViewController = [[ThirdViewController alloc] initWithNibName:@"ThirdViewController" bundle:nil]; [self.navigationController pushViewController:navigationController animated:YES completion:nil]; }

更多推荐

本文发布于:2023-08-07 21:15:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1465916.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:视图   几个   模态   堆栈   控制器

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!