简体   繁体   English

iPhone SDK-UIActionSheet-动态按钮标题

[英]iPhone SDK - UIActionSheet - Dynamic Button Titles

I have a requirement in an application where I need to be able to add otherButtonTitles dynamically, dependent upon some BOOL switches that a user has specified in the settings. 我在一个应用程序中有一个需求,我需要能够根据用户在设置中指定的一些BOOL开关来动态添加otherButtonTitles。 However, I can't seem to figure out how to go about doing this in the UIActionSheet initialization. 但是,我似乎无法弄清楚如何在UIActionSheet初始化中执行此操作。 I've tried to pass a NSString array (NSString[2]), and also a NSArray without any luck. 我试图传递一个NSString数组(NSString [2]),以及一个没有任何运气的NSArray。

Any help here is greatly appreciated. 非常感谢您的任何帮助。

The easiest way to do this that I have found is initially create your action sheet with no buttons, including no cancel or destructive button: 我发现最简单的方法是首先创建不带任何按钮的操作表,包括取消或破坏性按钮:

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

Then add a load of buttons as needed: 然后根据需要添加按钮的负载:

if(buttonX)
{
    [actionSheet addButtonWithTitle:@"Button X"];
}
if(buttonY)
{
    [actionSheet addButtonWithTitle:@"Button Y"];
}
if(buttonZ)
{
    [actionSheet addButtonWithTitle:@"Button Z"];
}

Then finally add the cancel button at the end and set the cancel button index: 然后最后在最后添加取消按钮并设置取消按钮索引:

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

Of course you can add both a cancel button and/or a destructive button in this way. 当然,您可以通过这种方式添加取消按钮和/或破坏性按钮。

You can add new buttons to the (already initialized) UIActionSheet with addButtonWithTitle: method. 您可以使用addButtonWithTitle:方法将新按钮添加到(已初始化的)UIActionSheet中。 You can also create your custom UIButtons and add them to UIActionSheet's view as a subViews 您还可以创建自定义UIButton并将它们作为subViews添加到UIActionSheet的视图中。

I ended up solving this by using some nil strings and an array. 我最终通过使用一些nil字符串和一个数组解决了这个问题。 I place the dynamic titles I need in an array, then loop through it and set the placeholder strings with as many titles as necessary. 我将所需的动态标题放置在数组中,然后遍历整个数组并为占位符字符串设置尽可能多的标题。 The placeholder strings are then passed to otherButtonTitles: in the action sheet initialization. 然后,在操作表初始化中,将占位符字符串传递给otherButtonTitles: Being otherButtonTitles: is terminated by nil, you can pass as many placeholder strings as necessary, as the first nil placeholder will terminate the rest. 由于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];

Hope this is helpful to others facing a similar dilemma. 希望这对面临类似困境的其他人有所帮助。

If you need that many buttons, create your own modal view and your own delegate protocol. 如果需要那么多按钮,请创建自己的模式视图和自己的委托协议。

Check the documentation for presentModalViewController:animated and dismissModalViewController:animated: 检查文档presentModalViewController:animateddismissModalViewController:animated:

When the user dismisses your modal view, your delegate can receive a method you build, something like customActionSheetDidFinish:(int)buttonChosen 当用户关闭您的模式视图时,您的委托人可以收到您构建的方法,例如customActionSheetDidFinish:(int)buttonChosen

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

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