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
}