开发者

Using addSubView or presentModalViewController in a standard method with paramaters?

I'm trying to reuse code, I have some tutorial view controllers / views, which I would like to call from an action sheet. However, the calling views are different. Sometimes the tutorial view(s) would need to be added as a subview and sometimes they would be added to the navigation controller.

How can I expand my standard function to cater for these two different situations ?

You can see what I'm having to do instead, which means duplicate code :(

I have a class called which holds the standard code, I want to add calls to views here directly.

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

I use in one view like this ...

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

And is another view like this...

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

}


Maybe the actionSheets could share one same delegate that would be a root viewController or the appDelegate that would know what to do according to it's current state. Hence the same actionSheet method would be used in both case.

If you put your appDelegate which can always be reached as the actionSheetDelegate, you should be able to gain control on every way to present your view, either modally or not in any of your views + the window.

EDIT (re-Edited with your code): Maybe try this

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];
    }
}

and in your 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];
 }

Didn't try it but it should work.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜