【问题标题】:Passing variable arguments conditionally有条件地传递变量参数
【发布时间】:2011-05-17 05:15:35
【问题描述】:

在我的一个 iPhone 应用程序中,我需要有条件地将变量参数发送到操作表。即

if(condition1)
    otherButtonTitles = @"Button1", @"Button2", nil
else
    otherButtonTitles = @"Button3", @"Button4", nil

UIActionSheet *mediaActionsSheet = [[UIActionSheet alloc] initWithTitle: nil
                                                             delegate: self
                                                    cancelButtonTitle: @"Cancel"
                                               destructiveButtonTitle: nil
                                                    otherButtonTitles: otherButtonTitles];

执行此操作的语法是什么? otherButtonTitles 的数据类型应该如何定义?

【问题讨论】:

    标签: objective-c iphone macos conditional-statements variadic-functions


    【解决方案1】:
    UIActionSheet * mediaActionsSheet;
    mediaActionSheet = [[UIActionSheet alloc] initWithTitle: nil
                                                   delegate: self
                                          cancelButtonTitle: @"Cancel"
                                     destructiveButtonTitle: nil
                                          otherButtonTitles:nil];
    
    
    if(condition1){
    
        [mediaActionsSheet addButtonWithTitle:@"Button1"];
        [mediaActionsSheet addButtonWithTitle:@"Button2"];
    }
    else {
    
        [mediaActionsSheet addButtonWithTitle:@"Button3"];
        [mediaActionsSheet addButtonWithTitle:@"Button4"];
    }
    

    【讨论】:

      【解决方案2】:

      你不能。 (嗯,这不是不可能的,但你不想那样做。)

      随便用

      UIActionSheet *mediaActionsSheet=nil;
      if(condition){
               mediaActionsSheet=[[UIActionSheet alloc] initWithTitle: nil
                                                               delegate: self
                                                      cancelButtonTitle: @"Cancel"
                                                 destructiveButtonTitle: nil
                                                      otherButtonTitles: @"button1",@"button2",nil];
      }else{
               mediaActionsSheet=[[UIActionSheet alloc] initWithTitle: nil
                                                               delegate: self
                                                      cancelButtonTitle: @"Cancel"
                                                 destructiveButtonTitle: nil
                                                      otherButtonTitles: @"button2",@"button3",nil];
      }
      

      如果你真的想构造一个变量来保存可变数量的参数,从this Wikipedia article开始。但你并不想这样做。我保证。

      【讨论】:

        【解决方案3】:

        您可以简单地在您的条件下创建操作表。

        UIActionSheet *mediaActionsSheet;
        if(condition1)
            {
           mediaActionsSheet  = [[UIActionSheet alloc] initWithTitle: nil
                                                                     delegate: self
                                                            cancelButtonTitle: @"Cancel"
                                                       destructiveButtonTitle: nil
                                       otherButtonTitles:@"Button1", @"Button2", nil];
        }
        
        else
        
        
        {
        mediaActionsSheet  = [[UIActionSheet alloc] initWithTitle: nil
                                                                 delegate: self
                                                         cancelButtonTitle: @"Cancel"
                                                      destructiveButtonTitle: nil
                                         otherButtonTitles:@"Button3", @"Button4", nil];
        }
        

        【讨论】:

          【解决方案4】:

          您可以创建 2 个操作表,并可以签入委托权限以执行合适的操作。 为什么不是 2 个操作表?

          类似: - (void)actionSheetUIActionSheet *)modalView clickedButtonAtIndexNSInteger)buttonIndex { 如果(模态视图==_firstActionSheetView) { } 别的 { } }

          【讨论】:

            猜你喜欢
            • 2011-09-13
            • 2013-04-25
            • 1970-01-01
            • 2011-04-13
            • 2013-12-06
            • 1970-01-01
            • 1970-01-01
            • 2021-09-11
            • 1970-01-01
            相关资源
            最近更新 更多