UIButton addTarget方法......不工作?

时间:2012-11-06 22:10:08

标签: objective-c ios uisegmentedcontrol uiactionsheet

我有一个类变量,它是UIActionSheet的一个实例,名为actionSheet。我添加了UIPickerView的实例以及UISegmentedControl的实例。虽然当我点击“完成”按钮(UISegmentedControl的实例)时,我的类方法dismissActionSheet永远不会被调用。任何人都可以帮助我吗?

//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

-(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];

}

1 个答案:

答案 0 :(得分:2)

方法dismissActionSheet没有任何参数,因此@selector应如下所示:

[closeButton addTarget:self action:@selector(dismissActionSheet) forControlEvents:UIControlEventTouchUpInside];