【问题标题】:how to display UIViewController from a NSObject class?如何从 NSObject 类显示 UIViewController?
【发布时间】:2012-10-10 03:53:22
【问题描述】:

有一个当前视图为UIViewController,它调用“LoginView”但我不在,我在一个NSObject 类中,我想调用,显示另一个UIViewController,它调用“MapView” ”。我该怎么做?

问题如上图所示。

【问题讨论】:

  • 两件事:你为什么要在模型中实例化和显示视图控制器,以及what have you tried?
  • AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; [[appDelegate loginViewController] presentViewController:mapViewController animated:YES completion:nil]; 我试过那个代码但没有用..
  • 您的 AppDelegate 类的名称真的是“AppDelegate”吗?此外,应该不需要从 NSObject 呈现 viewController !
  • 确定是“AppDelegate”。
  • 我找到了真正的代码片段。可能有人有同样的问题,所以我给他们。 AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; [[appDelegate window] addSubview:mapViewController.view]; [[appDelegate window] bringSubviewToFront:mapViewController.view];

标签: ios xcode4 uiviewcontroller storyboard nsobject


【解决方案1】:

在您的IBAction 或特定方法上写下这个:

UIWindow *window=[UIApplication sharedApplication].keyWindow;
UIViewController *root = [window rootViewController];

UIStoryboard *storyboard = root.storyboard;
CustomViewController *vcc =(CustomViewController *) [storyboard instantiateViewControllerWithIdentifier:@"storyBoardID"];

[root presentModalViewController:vcc animated:YES];

【讨论】:

    【解决方案2】:

    我假设您正在尝试从 NSObject 类的 UIViewController 类访问您的 UIViewController 成员。只需将 UIViewController 成员传递给 NSObject 类即可。在这种情况下是一个自我。让你做的是你可以改变、编辑、删除任何你想在另一个类的 UIView 中做的事情。下面是一个例子。

    从你的 UIViewController 类调用 NSObject 类

    @implementation myViewControllerClass
    
    - (void) viewDidLoad {
        //Pass in the UIViewController object, in this case  itself. 
        [[myNSOBjectClass alloc] startViewController:self]; 
        ....
    }
    

    然后从你的 NSObject 中

    @interface myNSOBjectClass{
        //Global access to the view controller. 
        UIViewController *viewController; 
    }
    ...
    
    @implementation myNSOBjectClass
    ...
    
    //function that the caller calls to pass their UIViewController object 
    - (void)startViewController:(UIViewController *)callerViewController{
        viewController = [[UIViewController alloc]init];
        //puts the caller's view controller to the global member.
        viewController = callerViewController;  
        ...
    }
    

    现在您的视图控制器触手可及!

    干杯:)!

    【讨论】:

    • @Lifematch 我有同样的问题,我发现这个答案真的很有用
    【解决方案3】:

    我在我的 NSObject 类中这样使用:

    [[[UIApplication sharedApplication] delegate].window.rootViewController presentViewController:yourMapViewContorller animated:YES completion:nil];
    

    希望对你有用。

    【讨论】:

    • UIapplication.shared.delegate.window 没有 rootViewController 你是如何在 NSObject 类中实现的?
    【解决方案4】:

    您不应该在模型中实例化和显示视图控制器。视图应该由模型驱动

    在这种情况下,您提到了LoginView 作为您的起点。当满足某些条件时(可能是成功登录?)您应该相应地更新底层模型,然后显示MapView

    来自LoginView

    MapView *mapView = [[MapView alloc] init];
    

    如果您的应用使用导航控制器:

    [self.navigationController pushViewController:mapView animated:YES];
    

    否则:

    [self presentViewController:mapView animated:YES completion:<nil or block>];
    

    【讨论】:

      【解决方案5】:

      试试这个代码。对你有帮助......

      在您的按钮单击操作中,您必须发送您的 UINavigationController 和当前 ViewController。因为 NSObject 类没有找到那个控制器。

      在您的按钮操作中输入以下代码:

      [demo login_method_called:self.navigationController withCurrentViewController:self];
      

      在您的 NSObject .h 类中输入以下代码:

      #import <Foundation/Foundation.h>
      #import "Home_ViewController.h"
      
      @interface Method_Action_class : NSObject
      
      - (void)login_method_called:(UINavigationController*)navigation  withCurrentViewController:(UIViewController*) controller;
      @end
      

      在您的 NSObject .m 类中输入以下代码:

      #import "Method_Action_class.h"
      
      @implementation Method_Action_class
      
      -(void)login_method_called:(UINavigationController*)navigation  withCurrentViewController:(UIViewController*) controller
      {
          Home_ViewController *home = [[Home_ViewController alloc] initWithNibName:@"Home_ViewController" bundle:nil];
          [navigation pushViewController:home animated:YES];
      }
      @end
      

      然后构建您的代码。

      【讨论】:

        【解决方案6】:

        我在视图控制器类中创建了一个 obersver,并声明了一个推送视图控制器的方法,并使用 NSObject 子类的 NSNotification 中心发布通知,它运行良好。

        在视图控制器中: [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(dismissPickerView) name:kNotificationDismissPicker object:nil];

        在 NSOject 的子类中: [[NSNotificationCenter defaultCenter] postNotificationName:kMoveToAchievementView object:nil];

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2014-10-19
          • 1970-01-01
          • 1970-01-01
          • 2013-03-15
          • 2019-09-17
          • 2018-07-13
          • 2011-10-17
          相关资源
          最近更新 更多