4

有人可以向我建议我如何拥有一个 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"];

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

4

2 回答 2

13

所以可以说我们有我们的NSArray声明

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

我们首先要创建一个 a 的实例,UIAlertView而不是设置任何按钮来otherButtonTitles离开它nil

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

现在我们有了我们的实例,我们将要遍历我们的NSArrayin afor loop以获取其中的每个对象,然后我们将这些对象中的每一个分配给一个NSStringwhich 使用我们可以在我们的标题addButtonWithTitle: UIAlertView上设置一个新按钮的方法UIAlertView已分配给我们的NSString.

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

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

[alert show];

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

只是一个观察

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

 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;
     }
 }

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

于 2013-11-01T09:00:21.100 回答
0

您可以使用 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];
于 2013-11-01T08:59:06.550 回答