0

我希望能够从不同的类打开 ViewController。所以我可以简单地调用它来在我需要的地方打开一个视图。

所以我在保存代码的类中有这个设置:


+ (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];

}

但是[self presentViewController:controller animated:YES completion:nil];给了我一个警告:

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

我可以NSLog从任何类中调用像这样的简单事物。但这不起作用。

4

1 回答 1

4

因为openCalcView 是一个Class 方法,所以没有UIViewController 实例(即没有[self presentViewController:] 方法)。

您还需要将 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]; 

} 

controller 参数表示打开新视图控制器的 UIViewController

于 2012-08-17T19:45:42.207 回答