【问题标题】:how to show alert with all messages from array [closed]如何显示来自数组的所有消息的警报[关闭]
【发布时间】:2014-01-23 18:16:12
【问题描述】:

我正在接收来自网络服务的消息,这些消息保存在 NSMutableArray 中。

如果我在数组中有 5 条消息,我需要在单个警报视图中显示所有消息。

NSString *temp; // Here there is only one message, i want to read all message from Array and feed to alert view.

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Info"
                            message: temp
                            delegate:nil
                            cancelButtonTitle:@"OK"
                            otherButtonTitles:nil];
[alertView show];

【问题讨论】:

    标签: ios ipad


    【解决方案1】:

    你可以用这个...

    NSString * temp = [yourArray componentsJoinedByString:@" "];
    

    注意:如果您需要像,:等任何连接组件,您可以进行相应的修改。如果您希望空间作为连接组件,请使用此@" "(single space), @"\n"(multiple lines)

    【讨论】:

    • 非常感谢,这显示了所有消息,有什么方法可以区分所有消息。它显示像第 1234 段一样,有些东西在 1 2 3 4 之间存在差异。
    • @user2813740 我认为这是最好的展示方式(1 2 3 4)。如果您指定格式,我会告诉您哪个可能或不可能?
    • 我的意思是它看起来像段落.. 我想知道我是否可以在按摩前做一些像 > 或 * 之类的事情,或者以另一种方式提供颜色......让它们更具可读性..基本上这些是给用户的通知消息......你明白我的意思..我知道我必须去定制可能是......但如果你有任何想法或任何示例代码......
    • @user2813740 我不明白你的段落风格。只显示示例文本(para 样式)。看到这个帖子来着色字符串。 stackoverflow.com/questions/14287386/…。尝试使用 NSAttributedString,它可能不起作用,我没有尝试过。因为initWithTitle:.... 的类型与NSAttributedString 不同。
    【解决方案2】:

    这是你的代码

    NSArray *alertArr = @[@"alert1", @"alert2", @"alert3", @"alert4", @"alert5"];
    NSString *temp;
    temp = [alertArr componentsJoinedByString:@"\n"];
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Info"
                                                        message: temp
                                                       delegate:nil
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil];
    [alertView show];
    

    【讨论】:

    • 为什么不只是temp = [alertArr componentsJoinedByString:@"\n"];
    • 感谢您的代码 deanWombourne
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-11
    • 1970-01-01
    • 2012-10-24
    • 2016-10-29
    • 1970-01-01
    • 2019-12-28
    • 2011-02-07
    相关资源
    最近更新 更多