UIActionSheet 取消按钮无法正常工作
UIActionSheet Cancel button is not working properly
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];
}
请帮我做这个,
您不应处理取消按钮按下操作。
由于 UIActionSheet
上的所有按钮按下操作都由 actionSheet:clickedButtonAtIndex:
处理,因此您需要检查按钮索引是否为取消按钮的索引。您可以使用 UIActionSheet
上的 cancelButtonIndex
执行此操作:
- (void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
if (actionSheet.cancelButtonIndex == buttonIndex) {
return;
}
}
你可以试试这个
- (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
}
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];
}
请帮我做这个,
您不应处理取消按钮按下操作。
由于 UIActionSheet
上的所有按钮按下操作都由 actionSheet:clickedButtonAtIndex:
处理,因此您需要检查按钮索引是否为取消按钮的索引。您可以使用 UIActionSheet
上的 cancelButtonIndex
执行此操作:
- (void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
if (actionSheet.cancelButtonIndex == buttonIndex) {
return;
}
}
你可以试试这个
- (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
}