有条件地传递变量参数

发布于 2024-11-07 20:08:49 字数 646 浏览 0 评论 0 原文

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

In one of my iPhone application, I need to send the variable arguments to the action sheet conditionally. i.e

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

What is the syntax to do this? How should the data type for otherButtonTitles be defined?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

峩卟喜欢 2024-11-14 20:08:49
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;
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"];
}
水晶透心 2024-11-14 20:08:49

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

只需使用

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

如果您确实想构造一个变量来保存可变数量的参数,请从 这篇维基百科文章。但你其实并不想那么做。我保证。

You can't. (Well, it's not impossible, but you don't want to do that.)

Just use

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

If you really want to construct a variable to hold variable number of arguments, start from this Wikipedia article. But you don't really want to do that. I promise.

暮凉 2024-11-14 20:08:49

那么您可以简单地根据您的情况创建操作表。

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

Well you could simply create the action sheet within your condition.

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];
}
手长情犹 2024-11-14 20:08:49

您可以创建 2 个操作表,并可以签入委托权以采取适当的操作。
为什么不是 2 个行动表?

类似的东西:
- (void)actionSheetUIActionSheet *)modalView clickedButtonAtIndexNSInteger)buttonIndex
{
if( modalView==_firstActionSheetView )
{
}
别的
{
}
}

you can create 2 action sheets and can check in delegate right for suitable action..
why not 2 action sheets ?

something like :
- (void)actionSheetUIActionSheet *)modalView clickedButtonAtIndexNSInteger)buttonIndex
{
if( modalView==_firstActionSheetView )
{
}
else
{
}
}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文