如何从NSObject类中显示UIViewController?

emy*_*emy 9 storyboard uiviewcontroller nsobject ios xcode4

有一个当前视图UIViewController,调用"LoginView",但我不在,我在一个NSObject类,我想打电话,显示另一个UIViewController调用"MapView".我怎样才能做到这一点?在此输入图像描述

问题就像上面的截图.

小智 10

根据您的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];
Run Code Online (Sandbox Code Playgroud)


Lif*_*tch 5

我假设您正在尝试从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]; 
    ....
}
Run Code Online (Sandbox Code Playgroud)

然后从你的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;  
    ...
}
Run Code Online (Sandbox Code Playgroud)

现在,您可以轻松获得视图控制器!

干杯:)!


Jos*_*ith 2

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

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

从内部LoginView

MapView *mapView = [[MapView alloc] init];
Run Code Online (Sandbox Code Playgroud)

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

[self.navigationController pushViewController:mapView animated:YES];
Run Code Online (Sandbox Code Playgroud)

否则:

[self presentViewController:mapView animated:YES completion:<nil or block>];
Run Code Online (Sandbox Code Playgroud)