0

假设我有这个代码:

if ([resultButton.titleLabel.text isEqualToString:@"Tax"]) {

        TAXViewController *controller = [[TAXViewController alloc]initWithNibName:@"TAXViewController" bundle:nil];
        controller.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
        [self presentViewController:controller animated:YES completion:nil];
        [controller release];

    }else if ([resultButton.titleLabel.text isEqualToString:@""]){

        RENTViewController *controller = [[RENTViewController alloc]initWithNibName:@"RENTViewController" bundle:nil];
        controller.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
        [self presentViewController:controller animated:YES completion:nil];
        [controller release];

    }else if //repeats a lot......

我想在多个 .h 文件中使用相同的代码。有没有一种方法可以编写代码(仅在一个地方)并从其他 .h 文件访问它而无需到处重复?

所以最后我猜,我可以将一个字符串传递给其他 .h/.m 文件,它会打开带有匹配字符串的视图。

4

1 回答 1

1

将它放在一个类中,并将您想要访问的所有其他文件放在它的子类中。

@interface SomeController : BaseViewController

你也可以简化你的代码

UIViewController *controller;
if ([resultButton.titleLabel.text isEqualToString:@"Tax"])
        controller = [[TAXViewController alloc]initWithNibName:@"TAXViewController" bundle:nil];
else if ([resultButton.titleLabel.text isEqualToString:@""])    
        controller = [[RENTViewController alloc]initWithNibName:@"RENTViewController" bundle:nil];
else if
....
//after all if-else statements
controller.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentViewController:controller animated:YES completion:nil];
[controller release];
于 2012-08-17T17:59:48.840 回答