Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iPhone SDK-UIActionSheet-动态按钮标题_Iphone_Objective C - Fatal编程技术网

iPhone SDK-UIActionSheet-动态按钮标题

iPhone SDK-UIActionSheet-动态按钮标题,iphone,objective-c,Iphone,Objective C,我在一个应用程序中有一个要求,我需要能够根据用户在设置中指定的一些BOOL开关动态添加其他按钮。但是,我似乎不知道如何在UIActionSheet初始化中执行此操作。我试图在没有任何运气的情况下传递NSString数组(NSString[2]),以及NSArray 非常感谢您的帮助。您可以使用addbuttonwhithtitle:方法将新按钮添加到(已初始化)UIActionSheet。您还可以创建自定义UIButtons并将其作为子视图添加到UIActionSheet的视图中如果您需要这么多

我在一个应用程序中有一个要求,我需要能够根据用户在设置中指定的一些BOOL开关动态添加其他按钮。但是,我似乎不知道如何在UIActionSheet初始化中执行此操作。我试图在没有任何运气的情况下传递NSString数组(NSString[2]),以及NSArray


非常感谢您的帮助。

您可以使用
addbuttonwhithtitle:
方法将新按钮添加到(已初始化)UIActionSheet。您还可以创建自定义UIButtons并将其作为子视图添加到UIActionSheet的视图中

如果您需要这么多按钮,请创建您自己的模式视图和代理协议

检查文档中是否有
presentModalViewController:animated
dismissModalViewController:animated:


当用户取消您的模式视图时,您的代理可以接收您构建的方法,类似于
customActionSheetDidFinish:(int)ButtonCosen

我最后使用了一些nil字符串和数组来解决这个问题。我将所需的动态标题放置在一个数组中,然后在其中循环,并根据需要使用尽可能多的标题设置占位符字符串。然后将占位符字符串传递到操作表初始化中的
otherButtonTitles:
。如果
otherButtonTitles:
以nil结尾,则可以根据需要传递任意多的占位符字符串,因为第一个nil占位符将终止其余占位符

// button titles    
NSMutableArray *buttons = [[NSMutableArray alloc] init];
[buttons addObject:@"Button 1"];
[buttons addObject:@"Button 2"];

// placeholders
NSString *button0 = nil, *button1 = nil, *button2 = nil;

// put together the buttons
for (int x = 0; x < buttons.count; x++) {
    switch (x) {
        case 0:
            button0 = [buttons objectAtIndex:x];
            break;
        case 1:
            button1 = [buttons objectAtIndex:x];
            break;
        case 2:
            button2 = [buttons objectAtIndex:x];
            break;
    }
}

// action sheet
UIActionSheet *option = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:button0, button1, button2, nil];
//按钮标题
NSMUTABLEARRY*按钮=[[NSMUTABLEARRY alloc]init];
[按钮添加对象:@“按钮1”];
[按钮添加对象:@“按钮2”];
//占位符
NSString*button0=nil,*button1=nil,*button2=nil;
//把按钮放在一起
对于(int x=0;x

希望这对其他面临类似困境的人有所帮助。

我发现,最简单的方法是最初创建没有按钮的行动表,包括没有取消或破坏性按钮:

UIActionSheet* actionSheet = [[UIActionSheet alloc] initWithTitle:@"Dynamic"
                                                        delegate:self
                                               cancelButtonTitle:nil
                                          destructiveButtonTitle:nil
                                               otherButtonTitles:nil];
然后根据需要添加大量按钮:

if(buttonX)
{
    [actionSheet addButtonWithTitle:@"Button X"];
}
if(buttonY)
{
    [actionSheet addButtonWithTitle:@"Button Y"];
}
if(buttonZ)
{
    [actionSheet addButtonWithTitle:@"Button Z"];
}
最后在末尾添加cancel按钮并设置cancel按钮索引:

[actionSheet addButtonWithTitle:@"Cancel"];
actionSheet.cancelButtonIndex = actionSheet.numberOfButtons - 1;

当然,您可以通过这种方式添加取消按钮和/或破坏性按钮。

我一直在使用标题为:
addbuttonw,然后添加一个“取消”按钮作为最后一个按钮,使用
setCancelButtonIndex
来识别它。问题是,当屏幕上的按钮数量超过屏幕所能容纳的数量时,屏幕会切换到UITableView,按钮会变成表格中的单元格。如果没有通过
cancelButtonTitle
指定实际的取消按钮,则无法关闭工作表,因为没有按钮出现。此时您必须退出应用程序。查看这些屏幕以获取示例:这很奇怪-我刚刚创建了包含20个按钮的UIActionSheet(所有按钮都添加了attButtonTitle:call)。该工作表将取消ok,并且会调用didDismissWithButtonIndex:和ClickedButtonIndex:委托方法。。。你使用的sdk版本是什么?如果你需要20或30个按钮,你会怎么做?好吧,我最多只需要5个,所以这适用于这个特定的应用程序。如果您需要20或30,这可能不实用。@harrywynn,您可能应该将此标记为答案(或至少其中一个)。请注意,取消按钮不会显示在UI\u用户界面\u惯用语()==UIUserInterfaceIdiomPad的位置。我花了20分钟试图弄明白为什么我的按钮在设置cancelButtonIndex后不会显示:)