有人可以向我建议我如何拥有一个包含数组按钮列表的UIAlertView.基本上,我想要以下代码,但是myArray是一个字符串数组,它们是多个按钮.
UIAlertView *alert = [[UIAlertView alloc]initWithTitle: @"Title"
message: nil
delegate: self
cancelButtonTitle:@"Cancel"
otherButtonTitles: myArray,nil];
[alert show];
Run Code Online (Sandbox Code Playgroud)
当然,我不能只是将数组放入UIAlertView中,因为它需要是一个字符串.如何将其转换为字符串,以便它不会只是一个按钮?或者甚至是我应该怎么做?
我尝试使用以下代码转换为字符串:
NSString *listToBeUsed = [myArray componentsJoinedByString:@","];
Run Code Online (Sandbox Code Playgroud)
但正如我所怀疑的那样,它没有用.我留下了一个长按钮,它们之间有一个字符串和逗号列表.
任何帮助,将不胜感激.
[编辑]
我的数组,如控制台中所示:
("888-555-5512","555-522-8243","(408)555-3514")
顺便说一句,电话号码来自模拟器联系人列表.没有真人的号码.
[编辑]
首先,我创建myArray:
NSMutableArray *myArray;
Run Code Online (Sandbox Code Playgroud)
然后我得到保存的NSUserDefault值,以防用户之前添加了电话号码(如果用户没有机会添加电话号码,则显然没有任何内容):
NSMutableArray *phoneNumber = [[NSUserDefaults standardUserDefaults] objectForKey:@"phoneNumber"];
Run Code Online (Sandbox Code Playgroud)
然后我用NSUserDefault启动myArray.如果没有保存,表格将为空,如果其中有电话号码,表格将显示它们,因为它显示myArray:
myArray = [[NSMutableArray alloc] initWithArray:phoneNumber];
Run Code Online (Sandbox Code Playgroud)
在- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
方法中,我包含以下代码以在表中显示myArray:
cell.numberLabel.text = [myArray objectAtIndex:indexPath.row];
Run Code Online (Sandbox Code Playgroud)
当用户编辑电话号码时,我将执行以下任一行代码:
[myArray addObject:phoneNumberSelected];
[myArray removeObjectAtIndex:indexPath.row];
Run Code Online (Sandbox Code Playgroud)
然后始终将数组保存在NSUserDefaults中,以便在用户返回时可以访问它:
[[NSUserDefaults standardUserDefaults] setObject:myArray forKey:@"phoneNumber"];
Run Code Online (Sandbox Code Playgroud)
我希望我不会错过任何事情.我不知道这是否足够的信息,不够或错误的信息.
Pop*_*eye 13
所以我们说我们已经NSArray
声明了
NSMutableArray *myArray = [[NSMutableArray alloc] initWithObjects:@"888-555-5512", @"555-522-8243", @"(408) 555-3514"];
Run Code Online (Sandbox Code Playgroud)
我们将首先创建一个a的实例,UIAlertView
而不是设置任何按钮来otherButtonTitles
保留它nil
UIAlertView *alert = [[UIAlertView alloc]initWithTitle: @"Title"
message: nil
delegate: self
cancelButtonTitle:@"Cancel"
otherButtonTitles: nil];
Run Code Online (Sandbox Code Playgroud)
现在我们有了我们的实例,我们将想要遍历我们NSArray
的a for loop
来获取其中的每个对象,我们将每个对象分配到一个NSString
使用该addButtonWithTitle:
UIAlertView
方法我们可以在我们UIAlertView
的标题上设置一个新按钮已分配到我们的NSString
.
for(NSString *buttonTitle in myArray) {
[alert addButtonWithTitle:buttonTitle];
}
Run Code Online (Sandbox Code Playgroud)
最后我们可以向用户显示警报.
[alert show];
Run Code Online (Sandbox Code Playgroud)
使用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;
}
}
Run Code Online (Sandbox Code Playgroud)
就像我说这只是一个观察,如果你愿意,你可以完全忽略这一点.
归档时间: |
|
查看次数: |
4972 次 |
最近记录: |