繁体   English   中英

有条件地传递变量参数

[英]passing variable arguments conditionally

在我的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的数据类型?

提前致谢。

此致Deepa

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

你不能 (嗯,这并非不可能,但您不想这样做。)

只需使用

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

如果您真的想构造一个变量来容纳可变数量的参数,请从Wikipedia文章开始。 但是您并不是真的想要这样做。 我承诺。

好吧,您可以简单地根据自己的条件创建操作表。

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

您可以创建2个操作表,也可以签入代表的权利以选择合适的操作。

像这样:-(void)actionSheetUIActionSheet *)modalView clickedButtonAtIndexNSInteger)buttonIndex {if(modalView == _ firstActionSheetView){} else {}}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM