2010-11-16 42 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

相關問題