简体   繁体   English

从其他类打开ViewController

[英]Open ViewController from different class

I want to be able to open a ViewController from a different class. 我希望能够从其他类打开ViewController。 So I could simply call it to open a view wherever I need it. 所以我可以简单地调用它以在需要时打开视图。

So I have this setup in the class that holds the code: 因此,在包含代码的类中有此设置:


+ (void)openCalcView: (NSString *)nameOfView {

UIViewController *controller;

if ([nameOfView isEqualToString:@"Tax"]) {

    controller = [[TAXViewController alloc]initWithNibName:@"TAXViewController" bundle:nil];


}else if ([nameOfView isEqualToString:@"Rent"]){

    controller = [[RENTViewController alloc]initWithNibName:@"RENTViewController" bundle:nil];

}

controller.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentViewController:controller animated:YES completion:nil];
[controller release];

}

But [self presentViewController:controller animated:YES completion:nil]; 但是[self presentViewController:controller animated:YES completion:nil]; gives me a warning: 给我一个警告:

Class method '+presentViewController:animated:completion:' not found (return type defaults to 'id')

I can call simple things like NSLog through this, from any class. 我可以从任何类中通过它调用诸如NSLog类的简单事物。 But this doesn't work. 但这是行不通的。

Because openCalcView is a Class method, there is no UIViewController instance (ie no [self presentViewController:] method). 因为openCalcView是一个Class方法,所以没有UIViewController实例(即没有[self presentViewController:]方法)。

You'll need to also pass a UIViewController to this class method, something like this: 您还需要将UIViewController传递给此类方法,如下所示:

+ (void)openCalcView: (NSString *)nameOfView fromViewController:(UIViewController *)controller { 

    UIViewController *newController; 

    if ([nameOfView isEqualToString:@"Tax"]) { 

        newController= [[TAXViewController alloc]initWithNibName:@"TAXViewController" bundle:nil]; 


    }else if ([nameOfView isEqualToString:@"Rent"]){ 

        newController= [[RENTViewController alloc]initWithNibName:@"RENTViewController" bundle:nil]; 

    } 

    newController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve; 
    [controller presentViewController:newController animated:YES completion:nil]; 
    [newController release]; 

} 

the controller parameter represents the UIViewController that is opening the new view controller controller参数代表正在打开新视图控制器的UIViewController

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM