【问题标题】:UIAlertView button arrayUIAlertView 按钮数组
【发布时间】:2013-11-01 08:50:33
【问题描述】:

有人可以向我建议我如何拥有一个 UIAlertView,其中包含来自数组的按钮列表。 基本上,我想要以下代码,但其中 myArray 是一个字符串数组,以多个按钮的形式出现。

UIAlertView *alert = [[UIAlertView alloc]initWithTitle: @"Title"
                                               message: nil
                                              delegate: self
                                     cancelButtonTitle:@"Cancel"
                                     otherButtonTitles: myArray,nil];
[alert show];

当然,我不能只在 UIAlertView 中放入一个数组,因为它需要是一个字符串。如何将其转换为字符串,以使其不只是一个按钮?还是我应该这样做?

我尝试使用以下代码转换为字符串:

NSString *listToBeUsed = [myArray componentsJoinedByString:@","];

但正如我所怀疑的那样,它没有用。我留下了一个长按钮,其中包含字符串列表和逗号。

任何帮助将不胜感激。

[编辑]

我的数组,如控制台所示:

( "888-555-5512", "555-522-8243", “(408) 555-3514” )

顺便说一下,电话号码来自模拟器联系人列表。没有真实的人数。

[编辑]

首先,我创建了 myArray:

NSMutableArray *myArray;

然后我得到保存的 NSUserDefault 值,以防用户之前添加了电话号码(如果用户没有机会添加电话号码,显然将一无所有):

NSMutableArray *phoneNumber = [[NSUserDefaults standardUserDefaults] objectForKey:@"phoneNumber"];

然后我使用 NSUserDefault 启动 myArray。如果其中没有保存任何内容,则表格将为空,如果其中有电话号码,则表格将显示它们,因为它显示了 myArray:

myArray = [[NSMutableArray alloc] initWithArray:phoneNumber];

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 方法中,我包含以下代码以在表中显示 myArray:

cell.numberLabel.text = [myArray objectAtIndex:indexPath.row];

当用户编辑电话号码时,我将执行以下任一代码行:

[myArray addObject:phoneNumberSelected];
[myArray removeObjectAtIndex:indexPath.row];

然后始终将数组保存在 NSUserDefaults 中,以便用户返回时可以访问:

[[NSUserDefaults standardUserDefaults] setObject:myArray forKey:@"phoneNumber"];

我希望我没有错过任何东西。我不知道这是否是足够的信息,不够或错误的信息。

【问题讨论】:

标签: ios objective-c


【解决方案1】:

假设我们将NSArray 声明为

NSMutableArray *myArray = [[NSMutableArray alloc] initWithObjects:@"888-555-5512", @"555-522-8243", @"(408) 555-3514"];

我们首先要创建一个UIAlertView 的实例,但不要将任何按钮设置为otherButtonTitles,只需将其保留为nil

UIAlertView *alert = [[UIAlertView alloc]initWithTitle: @"Title"
                                               message: nil
                                              delegate: self
                                     cancelButtonTitle:@"Cancel"
                                     otherButtonTitles: nil];

现在我们有了我们的实例,我们将要在 for loop 中循环遍历我们的 NSArray 以获取其中的每个对象,然后我们将每个对象分配给使用 NSString 的 @987654330 @UIAlertView 方法我们可以在UIAlertView 上设置一个新按钮,其标题是我们分配给NSString 的标题。

for(NSString *buttonTitle in myArray) {
    [alert addButtonWithTitle:buttonTitle];
}

最后我们可以向用户显示警报。

[alert show];

使用NSString *listToBeUsed = [myArray componentsJoinedByString:@","]; 只会将NSArray 中的所有对象加入NSString 分隔的“,”所以除非你打算用那个长字符串I 制作一个按钮,我怀疑这不是你的计划它不会工作。

只是观察

这只是一个观察,但您提供的字符串看起来像是电话号码,以使您的代码更好地工作,这样做不是更好吗

 for(NSString *buttonTitle in myArray) {
     if([[UIApplication sharedApplication] canOpenURL:[[NSURL URLWithString:[NSString stringWithFormat:@"tel://%@", buttonTitle] stringByReplacingOccurrencesOfString:@"-" withString:@""]]) {
         // The reason for this if statement is to check whether we can open a URL (Dialer) 
         // This is because what if the user was on an iPad or iTouch that can't use the dialer.
         [alert addButtonWithTitle:buttonTitle];
     } else {
         // Else do what ever you want to tell the user they can't make a phone call.
         break;
     }
 }

就像我说的,这只是一个观察结果,如果你愿意,你可以完全忽略它。

【讨论】:

  • 好的,所以当我将该代码直接放入我拥有的代码中时,我会收到以下警告Collection expression type 'NSString *' may not respond to 'countByEnumeratingWithState:objects:count:'。我需要替换该代码中的某些内容吗?
  • @user2329585 你如何初始化你的数组?这表明myArrayNSString 而不是NSArray
  • myArray = [[NSMutableArray alloc] initWithArray:phoneNumber];(phoneNumber 是表中的数据列表)我刚刚意识到它是一个 NSMutableArray,这与它有什么关系吗?我忘记了。
  • @user2329585 不,在这种情况下这并不重要,除非您删除/添加对象,但我会修改我的代码。
  • @user2329585 phoneNumber 是如何初始化的?
【解决方案2】:

您可以使用 addButtonWithTitle 方法添加其他按钮,使用 for 语句。

    [alert addButtonWithTitle:@"AAA"];

例如参考以下:-

NSArray *myArr = [NSArray arrayWithObjects:@"888-555-5512",@"456", nil];

UIAlertView *view = [[UIAlertView alloc] initWithTitle:@"ddd" message:@"dssd" delegate:self cancelButtonTitle:@"sdds" otherButtonTitles:nil];

for(NSString* number in myArr)
    [view addButtonWithTitle:number];

[view show];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-21
    • 2013-07-17
    • 2011-06-28
    相关资源
    最近更新 更多