ios - 访问单个字符串参数(UIAlertView otherButtonTitles)

标签 ios nsstring uialertview

我正在为应用程序编写自己的自定义警报 View 。客户对警报类型消息有自己的外观/感觉。我读过,子类化 UIAlertView 并不是一个好主意,所以我只是想让我正在使用的当前消息 View 更加动态,这样我就可以更多地重用它.

好的,我的问题。我从 UIAlertView 窃取 init 来初始化我自己的自定义警报。

- (id)initWithTitle:(NSString *)title message:(NSString *)message delegate:(id)delegate cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ... NS_REQUIRES_NIL_TERMINATION;

这直接取自 Apple 的 UIAlertView 初始值设定项。我的也能正常工作,所以为什么不直接使用他们的 init 呢。

我希望能够为 otherButtonTitles 传递多个字符串,就像 UIAlertView 一样。我的问题是,如何访问传入的字符串?

最佳答案

我可能会将 otherButtonTitles 参数设置为 NSArray 而不是 NSString。从那里,您可以根据数组的计数动态生成其他按钮,并将所述按钮的标题设置为存储在数组当前索引中的 NSString。

编辑:我想我在 UIAlertView.m 中找到了一些有希望的东西(不确定它是否真的来自 Apple),但它也将 otherButtonTitles 作为字符串。它使用 va_list 来存储 otherButtonTitles,然后将对象添加到包含所有按钮的可变数组中,包括索引 0 处的取消按钮(如果适用)。

https://github.com/BigZaphod/Chameleon/blob/master/UIKit/Classes/UIAlertView.m

以下是摘录:

 if (otherButtonTitles) {
        [self addButtonWithTitle:otherButtonTitles];

        id buttonTitle = nil;
        va_list argumentList;
        va_start(argumentList, otherButtonTitles);

        while ((buttonTitle=(__bridge NSString *)va_arg(argumentList, void *))) {
            [self addButtonWithTitle:buttonTitle];
        }

        va_end(argumentList);
    }

关于ios - 访问单个字符串参数(UIAlertView otherButtonTitles),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14199987/

相关文章:

ios - Swift 使用函数中指定的守卫和 fatal error

iphone - 更改任何单元格名称时顶部单元格名称也会更改

ios - 如何使用 CFStringTransform 在没有变音符号的情况下正确音译西里尔字母?

ios - 将 UITextView 中的文本替换为可链接文本 - iOS

ios - iOS UIAlertView 中未使用的表达式结果

objective-c - 为什么 UIAlertView 会有所不同?

ios - 如何删除 YandexMapKit 中的分类地标?

ios - 如何从 NSString 中正确随机化字母

iOS7如何改变UIAlertView的坐标

ipad - 在 iPad 上绘制 2D Sprite 的 GL_BLEND 问题