当前位置:  开发笔记 > IOS > 正文

在iOS中从提供的视图控制器推送视图

如何解决《在iOS中从提供的视图控制器推送视图》经验,为你挑选了5个好方法。

简而言之: 我如何从呈现的ViewController中推送PushController?

简单来说 :

我有MainViewController,我点击按钮时有一个按钮,我正在展示一个叫做的视图LoginViewController.

在这个页面上(LoginViewController),我再次button点击它,我尝试推动我的视图控制器(称为HomeViewController)它不会推动.

这是我的代码片段,

MainViewController.m

- (IBAction)LoginClicked:(id)sender {
    LoginViewController *vc = [[LoginViewController alloc] init];
    [self presentViewController:vc animated:YES completion:nil];
}

LoginViewController.m

- (IBAction)buttonActionMethodOnLoginView:(id)sender{
     NSLog(@"viewControllers %@",APPDELEGATE.nav.viewControllers);
     //LoginViewController is not in this array
     HomeViewController *obj = [[HomeViewController alloc] init];
     [self.navigationController pushViewController:obj animated:YES];
}

但它对我不起作用.另外,我a stack of view controllers之前打印过pushed,但它没有LoginViewController.因此,无需添加LoginViewControllera stack of view controllers,我怎样才能pushedHomeViewControllerLoginViewController

当我从中获取时HomeViewController,LoginViewController应该打开..

是否有可能使用这个单身NavigationController

注意: -这里,我刚刚使用Login,Home和Main ViewController进行了一个示例.但我希望它进入其他屏幕.



1> Shubham bair..:

嗨,当你在演示登录视图控制器只是提出一个navigationController,如:

LoginVC *loginVCObj =[[LoginVC alloc]initWithNibName:@"LoginVC" bundle:nil];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:loginVCObj];
[self presentViewController:nav animated:YES completion:nil];

现在您的PresentedViewController是一个导航控制器,现在您可以简单地推送到您的Home VC

  HomeViewController *obj = [[HomeViewController alloc] init];
 [self.navigationController pushViewController:obj animated:YES];

希望它对你有所帮助


@Shubhambairagi你把什么放进了完工区?我的天啊

2> Vive..:

LoginViewController不应该被推到导航控制器堆栈.让我在下面描述"为什么".

我们MainViewController应该在堆栈上 - 你总是想回到那里.

// AppDelegate.m (only if you don't use storyboards, if you do - you don't need to copy this part of code)
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // create the window
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    [self.window setBackgroundColor:[UIColor whiteColor]];
    [self.window makeKeyAndVisible];

    // set view controllers
    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:[[MainViewController alloc] init]];
    [self.window setRootViewController:navigationController];
}

具体行动表明LoginViewController.您不希望用户能够点按back并转到MainViewController.之后,您将不希望用户返回LoginViewController.因此,您需要将其显示为模态:

// inside `MainViewController.m`
- (IBAction)myCoolActionToShowLogin:(id)sender {
    [self presentViewController:[[LoginViewController alloc] init] animated:YES completion:nil];
}

现在我们可以看到LoginViewController.当用户完成登录时,将其关闭并显示HomeViewController:

// inside `LoginViewController.m`
- (IBAction)myAwesomeActionToShowHome:(id)sender {
    UINavigationController *navigationController = (UINavigationController *)[UIApplication.sharedApplication.keyWindow rootViewController];
    [navigationController pushViewController:[[HomeViewController alloc] init] animated:YES];
    [self dismissViewControllerAnimated:YES completion:nil];
}

笔记:

正如您可能注意到的那样,myAwesomeActionToShowHome:您希望您拥有导航控制器rootViewController.这是有效的,但应该更好 - 你应该检查导航是否实际上是导航控制器,而不是铸造它.或者您可以创建一个委托或块来推送新的委托或块.这是最快,最简单的工作解决方案,应该在以后进行改进.

真的应该阅读:Apple Developer - >"View Controller Programming"文档,因为这些是您应该了解的正确开发和设计UX 的核心基础知识.

这是工作演示示例.



3> Hemang..:

您无法从显示的视图控制器推送.我建议你应该保持你的导航层次结构.

为此,您应该从MainViewController中呈现LoginViewController,并且应该为MainViewController传递导航控制器.

- (IBAction)openLogin:(id)sender {
    LoginViewController *loginVC = (LoginViewController *) [self.storyboard instantiateViewControllerWithIdentifier:@"login"];
    [loginVC setReferencedNavigation:self.navigationController];
    [self presentViewController:loginVC animated:YES completion:nil];
}

然后在LoginViewController里面,你应该像这样推送到HomeViewController,

LoginViewController.h

@interface LoginViewController : UIViewController {
    UINavigationController *refNavigationController;
}
- (void) setReferencedNavigation:(UINavigationController *)refNavCon;

LoginViewController.m

- (void) setReferencedNavigation:(UINavigationController *)refNavCon {
    refNavigationController = refNavCon;
}

- (IBAction)openHome:(id)sender {
    [self dismissViewControllerAnimated:YES completion:^{
        UIViewController *homeVC = [self.storyboard instantiateViewControllerWithIdentifier:@"home"];
        [refNavigationController pushViewController:homeVC animated:YES];
    }];
}

通过这样做,看起来,你是从LoginViewController推动,但实际上你是从MainViewController推动.

您可以自定义此方法以维护此流的动画和UI.

在此输入图像描述



4> Rohit Pradha..:

1)将navigation controller with its根视图控制器设置为视图控制器.

- (IBAction)LoginClicked:(id)sender 
{
    LoginViewController *loginViewController = [LoginViewController alloc] init];
    UINavigationController *navController = [UINavigationController alloc] initWithRootViewController:loginViewController];
    [self presentViewController:navController animated:YES completion:nil];
}

- (IBAction)buttonActionMethodOnLoginView:(id)sender
{
    HomeViewController *obj = [[HomeViewController alloc] init];
    [self.navigationController pushViewController:obj animated:YES];
}

希望它能为你效劳.



5> ukim..:

问题是LoginViewController没有导航控制器.然后你给它一个.

MainViewController.m

创建一个UINavigationController,放入LoginViewController堆栈并呈现UINavigationController.

- (IBAction)LoginClicked:(id)sender {
    LoginViewController *vc = [[LoginViewController alloc] init];
    UINavigationController = nav = [[UINavigationController alloc] init];
    nav.viewControllers = @[vc];
    [self presentViewController:nav animated:YES completion:nil];
}

LoginViewController.m

- (IBAction)buttonActionMethodOnLoginView:(id)sender{
    HomeViewController *obj = [[HomeViewController alloc] init];
    [self.navigationController pushViewController:obj animated:YES];
}

解雇

打电话给dismissViewControllerAnimatedMainViewController.

推荐阅读
ERIK又
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有