UIButton addTarget方法...不工作?
问题描述:
我有一个类变量,是的UIActionSheet
一个实例,称为actionSheet。我添加了一个UIPickerView
的实例以及一个UISegmentedControl
的实例。虽然当我点击“完成”按钮(UISegmentedControl的实例)时,我的类方法dismissActionSheet永远不会被调用。谁能帮我吗?UIButton addTarget方法...不工作?
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
-(IBAction)chooseTopicButtonPressed{
actionSheet = [[UIActionSheet alloc] initWithTitle:nil
delegate:nil
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
[actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
CGRect pickerFrame = CGRectMake(0, 40, 0, 0);
UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:pickerFrame];
pickerView.showsSelectionIndicator = YES;
pickerView.dataSource = singlePicker.dataSource;
pickerView.delegate = singlePicker.delegate;
[actionSheet addSubview:pickerView];
UISegmentedControl *closeButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:@"Done"]];
closeButton.momentary = YES;
closeButton.frame = CGRectMake(10, 6, 300, 30);
closeButton.segmentedControlStyle = UISegmentedControlStyleBar;
closeButton.tintColor = [UIColor blueColor];
[closeButton addTarget:self action:@selector(dismissActionSheet:) forControlEvents:UIControlEventTouchUpInside];
//dismissActionSheet NOT BEING CALLED FOR SOME REASON!!!
[actionSheet addSubview:closeButton];
[actionSheet showInView:[[UIApplication sharedApplication] keyWindow]];
[actionSheet setBounds:CGRectMake(0, 0, 320, 485)];
}
-(void)dismissActionSheet{
NSLog(@"dismissActionSheet called");
[actionSheet dismissWithClickedButtonIndex:0 animated:YES];
}
答
dismissActionSheet
没有任何参数,以便在@selector
应该是如下的方法:
[closeButton addTarget:self action:@selector(dismissActionSheet) forControlEvents:UIControlEventTouchUpInside];
这是一个很大的控制滥用。 'UIActionSheet'是为了显示一个按钮菜单。 'UISegmentedControl'并不是真的用作一般情况下的按钮。其中一天,苹果可能会更新“UIActionSheet”的实现,并且每个使用错误方式的人都会遇到很多问题。尝试使用真正的UIButton而不是'UISegmentedControl'或尝试使用'UIActionSheet'中的某个按钮。 – rmaddy