在带有参数的标准方法中使用addSubView或presentModalViewController? [英] Using addSubView or presentModalViewController in a standard method with paramaters?

查看:49
本文介绍了在带有参数的标准方法中使用addSubView或presentModalViewController?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试重用代码,我有一些教程视图控制器/视图,我想从操作表中调用它们。但是,调用视图不同。有时需要将教程视图添加为子视图,有时又需要将其添加到导航控制器。

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

}


推荐答案

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

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.

如果您将appDelegate始终作为actionSheetDelegate来访问,则应该能够控制每个

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:

MyAppDelegate.h:

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

- (void) showHelp;

MyAppDelegate.m:

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中:

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.

这篇关于在带有参数的标准方法中使用addSubView或presentModalViewController?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆