我使用Objective-C创建了活动表。当我想获得所选的按钮标题时,我卡住了。请帮我。 (我知道有一种委托方法来获取操作的标题。但它在iOS 8.0+中已被弃用。) 这是我的代码。我的要求是获取所选标题并将其更新为文本字段。
NSArray *subjectType = [NSArray arrayWithObjects:@"Customer Service",@"Product Info",@"Deals",@"Special",@"Others", nil];
UIAlertController *subjectSheet = [UIAlertController alertControllerWithTitle:@"Subjects" message:@"Select a prefered subject" preferredStyle:UIAlertControllerStyleActionSheet];
for (int i=0; i<subjectType.count; i++) {
UIAlertAction *myalertAction = [UIAlertAction actionWithTitle:[subjectType objectAtIndex:i] style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
}];
[subjectSheet addAction:myalertAction];
}
答案 0 :(得分:1)
尝试UIAlertAction
UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"Title"
message:@"message"
preferredStyle:UIAlertControllerStyleActionSheet];
[alert addAction:[UIAlertAction actionWithTitle:@"OK"
style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
NSLog(@"%@",action.title);
}]];
[self presentViewController:alert animated:true completion:nil];
答案 1 :(得分:1)
你可以做下面的事情来实现你想要实现的目标
NSArray *subjectType = [NSArray arrayWithObjects:@"Customer Service",@"Product Info",@"Deals",@"Special",@"Others", nil];
UIAlertController *subjectSheet = [UIAlertController alertControllerWithTitle:@"Subjects" message:@"Select a prefered subject" preferredStyle:UIAlertControllerStyleActionSheet];
for (int i=0; i<subjectType.count; i++) {
UIAlertAction *myalertAction = [UIAlertAction actionWithTitle:[subjectType objectAtIndex:i] style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
[self takeActionAccordingToString:action.title];
}];
[subjectSheet addAction:myalertAction];
[self presentViewController:subjectSheet animated:YES completion:nil];
}
-(void)takeActionAccordingToString:(NSString *)actionTitle{
if([actionTitle isEqualToString:[yourArray objectAtIndex:0]]){
//you can take action here
}
if([actionTitle isEqualToString:[yourArray objectAtIndex:1]]){
//you can take action here
}
if([actionTitle isEqualToString:[yourArray objectAtIndex:2]]){
//you can take action here
}
if([actionTitle isEqualToString:[yourArray objectAtIndex:3]]){
//you can take action here
}
if([actionTitle isEqualToString:[yourArray objectAtIndex:4]]){
//you can take action here
}
//etc....
}