从不同的类打开ViewController

时间:2012-08-17 19:42:56

标签: objective-c ios cocoa-touch

我希望能够从不同的类中打开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之类的简单内容。但这不起作用。

1 个答案:

答案 0 :(得分: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