2010-11-16 50 views
0

我想重用代码,我有一些教程视图控制器/视图,我想从操作表调用。但是,调用视图是不同的。有时,教程视图将需要添加为子视图,有时它们将被添加到导航控制器。在具有参数的标准方法中使用addSubView或presentModalViewController?

如何扩展我的标准功能以迎合这两种不同情况?

你可以看到我在做相反,这意味着重复的代码:(

我有一个叫做类拥有标准的代码,我想在这里呼吁直接添加到的意见是什么。

-(void)showHelpClickButtonAtIndex:(int)buttonIndex:(UIView *)vw { 
if (buttonIndex == CommonUIHelpPagesBtnIdx) { 
    // do nothing 
} else if (buttonIndex == 0) { 
    NSLog(@"Tutorial here"); 
} 
} 

我在这样一个视图中使用...

- (void)actionSheet:(UIActionSheet *)actionSheet 
     clickedButtonAtIndex:(NSInteger)buttonIndex { 
CommonUI *cui = [CommonUI alloc]; 
[cui showHelpClickButtonAtIndex:buttonIndex:self.view]; 
[cui release]; 

if (buttonIndex == CommonUIHelpPagesBtnIdx) { 
    UIViewController *theController = [[HelpViewController alloc] 
     initWithNibName:@"HelpView" 
     bundle:nil onPage:HelpPageCalcBalance]; 
    [self.navigationController.topViewController 
     presentModalViewController:theController animated:YES]; 
    [theController release]; 
    } 
} 

,是这样的另一种观点认为...

- (void)actionSheet:(UIActionSheet *)actionSheet 
    clickedButtonAtIndex:(NSInteger)buttonIndex { 
    [cui showHelpClickButtonAtIndex:buttonIndex:self.view]; 

    if (buttonIndex == CommonUIHelpPagesBtnIdx) { 
     theController = [[HelpViewController alloc] initWithNibName:@"HelpView"      
     bundle:nil onPage:HelpPageGettingStarted]; 

     [self.view addSubview:theController.view]; 
    } 

} 

回答

0

也许actionSheets可以共享一个相同的委托,它可以是根viewController或appDelegate,它可以根据它的当前状态知道该怎么做。因此,在这两种情况下都会使用相同的actionSheet方法。

如果你把你的appDelegate始终作为actionSheetDelegate到达,你应该能够控制每一种方式来呈现你的视图,无论是在你的视图还是窗口中。

EDIT(与您的代码重新编辑):也许试试这个

MyAppDelegate.h:

@interface MyAppAppDelegate : NSObject <UIApplicationDelegate, UIActionSheetDelegate> 
(...) 

- (void) showHelp; 

MyAppDelegate.m:

- (void) showHelp { 
    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"" 
                  delegate:self 
                cancelButtonTitle:@"Cancel" 
               destructiveButtonTitle:nil 
                otherButtonTitles: @"Tutorial", @"Help Pages", nil]; 

    actionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque; 
    [actionSheet showInView:window]; 
    [actionSheet release]; 
} 

- (void)actionSheet:(UIActionSheet *)actionSheet 
clickedButtonAtIndex:(NSInteger)buttonIndex { 
    if (buttonIndex == CommonUIHelpPagesBtnIdx) { 
     UIViewController *theController = [HelpViewController alloc]; 
     if ([[self lvc] superview] != nil) { 
      theController = [initWithNibName:@"HelpView"      
             bundle:nil 
             onPage:HelpPageGettingStarted]; 
      [window addSubview:theController.view]; 
     } else { 
      theController = [initWithNibName:@"HelpView" 
             bundle:nil 
             onPage:HelpPageCalcBalance]; 
      UINavigationController * navController = [tabBarController selectedViewController]; 
      [navController.topViewController presentModalViewController:theController animated:YES]; 
     } 
     [theController release]; 
    } 
} 

,并在您viewControllers:

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    MyAppDelegate *delegate = (MyAppDelegate *) [[UIApplication sharedApplication] delegate]; 

    UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeInfoLight]; 
    [infoButton addTarget:delegate 
        action:@selector(showHelp) 
     forControlEvents:UIControlEventTouchUpInside]; 
    UIBarButtonItem *infoItem = [[UIBarButtonItem alloc] initWithCustomView:infoButton]; 
    self.navigationItem.rightBarButtonItem = infoItem; 
    [infoItem release]; 
} 

没有尝试,但它应该工作。

+0

从你的代码,我相信调用viewController发送自己作为委托的参数,也许如果你不尝试这样做,并总是使用像你的appDelegate相同的代表,这可能会有所帮助。 – ebany 2010-11-16 12:50:55

+0

如果这与tabBar问题相同,如果您将ApplicationDelegate设置为UIActionSheetDelegate,并将其设置为委托([[UIApplication sharedApplication]委托]),则它可以响应您的actionSheets方法并知道该怎么做。既可以在窗口中显示视图,也可以将(从模态到或不从)导航控制器的topViewController中的视图从TabBarController的选定选项卡中检索。 – ebany 2010-11-16 13:04:34

+0

刚刚编辑我的答案,让我知道如果这有助于 – ebany 2010-11-16 13:45:07

相关问题