UIActionSheet
,它有标题的按钮,我从数组中获取标题。我想获取按钮标题并显示在UILabel
,我做了,但如果我按下取消按钮取消按钮也显示,我不想在UILabel
中显示取消按钮标题
以下代码,我试过,
- (IBAction)site_Selection:(id)sender {
NSArray *array = @[@"one",@"two",@"three",@"Smart Gladiator1"];
UIActionSheet *actionSheet = [[UIActionSheet alloc]
initWithTitle:nil
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
actionSheet.delegate = self;
for (NSString *title in array) {
[actionSheet addButtonWithTitle:title];
}
actionSheet.cancelButtonIndex = [actionSheet addButtonWithTitle:@"Cancel"];
[actionSheet showInView:self.view];
}
-(void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
NSString *title = [actionSheet buttonTitleAtIndex:buttonIndex];
self.btn_site_selection.titleLabel.text = [actionSheet buttonTitleAtIndex:buttonIndex];
}
请帮我这样做,
答案 0 :(得分:2)
您不应按下取消按钮。
由于UIActionSheet
上的所有按钮都由actionSheet:clickedButtonAtIndex:
处理,因此您需要检查按钮索引是否为取消按钮的索引。您可以使用cancelButtonIndex
上的UIActionSheet
:
- (void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
if (actionSheet.cancelButtonIndex == buttonIndex) {
return;
}
}
答案 1 :(得分:0)
您可以尝试
- (IBAction)actionSheetCall:(id)sender {
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Confirm call"
message:@"Are you sure to call?"
preferredStyle:UIAlertControllerStyleActionSheet]; // 1
UIAlertAction *secondAction = [UIAlertAction actionWithTitle:@"Cancel"
style:UIAlertActionStyleCancel handler:^(UIAlertAction * action) {
NSLog(@"You pressed cancel button ");
}]; // 3
for(int i = 0; i < [Arr_phone count]; i++) {
UIAlertAction *firstAction = [UIAlertAction actionWithTitle:[activity.Arr_phone objectAtIndex:i]
style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
NSLog(@"You pressed Logout one");
//[self setupCall];
[self incomingCall:[Arr_phone objectAtIndex:i]];
}]; // 2
[alert addAction:firstAction]; // 4
}
[alert addAction:secondAction]; // 5
[self presentViewController:alert animated:YES completion:nil]; // 6
}