动态创建的UIBarButtonItem不会触发

时间:2014-11-24 20:27:37

标签: ios objective-c uipickerview uitoolbar

我有一个UIPickerView,在父视图的加载下附加了一个UIToolbar:

- (void)viewDidLoad {
    [super viewDidLoad];

    self.itemSortPicker.hidden = false;

    pickerData = @[@"Name",@"Item Level",@"Crafting Level",@"Profit",@"Profit Percentage",@"Sell Price",@"Buy Price",@"Supply",@"Demand",@"Rarity"];

    self.itemSortPicker.dataSource = self;
    self.itemSortPicker.delegate = self;


    UIToolbar *pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];

    pickerToolbar.barStyle = UIBarStyleDefault;
    [pickerToolbar sizeToFit];

    [pickerToolbar setFrame:CGRectMake(0, -pickerToolbar.bounds.size.height, 320, 44)];

    NSMutableArray *barItems = [[NSMutableArray alloc] init];


    UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(pickerCancel)];
    [barItems addObject:cancelBtn];

    UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
    [barItems addObject:flexSpace];

    UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(pickerDone)];
    [barItems addObject:doneBtn];

    [pickerToolbar setItems:barItems animated:YES];

    CGRect pickerRect = self.itemSortPicker.bounds;
    self.itemSortPicker.bounds = pickerRect;
    self.itemSortPicker.frame = CGRectMake(0, 44, 320, 216);

    [self.itemSortPicker addSubview:pickerToolbar];

}

现在的问题是,当我点击完成的'或者'取消'按钮,相关方法不会触发。该方法不采用任何参数,只是执行NSLog操作。 PickerView没有从栏中获得焦点,因为当我点击并拖动按钮时,PickerView不会改变。

2 个答案:

答案 0 :(得分:1)

我不确定问题是因为按钮或工具栏本身还是pickerview,但更好的方法是以编程方式完成所有操作并且必须从视图中删除pickerview:

- (void)viewDidLoad {
    [super viewDidLoad];

    pickerData = @[@"Name",@"Item Level",@"Crafting Level",@"Profit",@"Profit Percentage",@"Sell Price",@"Buy Price",@"Supply",@"Demand",@"Rarity"];

    UIPickerView *itemSortPicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, 320, 162)];
    itemSortPicker.dataSource = self;
    itemSortPicker.delegate = self;
    itemSortPicker.showsSelectionIndicator = YES;

    UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(pickerCancel)];
    UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
    UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(pickerDone)];

    UIToolbar *pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
    [pickerToolbar setBarStyle:UIBarStyleDefault];
    [pickerToolbar sizeToFit];
    [toolBar setItems:@[cancelBtn,flexSpace,doneBtn]];

    BlaBlaTextField.delegate = self;
    BlaBlaTextField.inputView = itemSortPicker;
    BlaBlaTextField.inputAccessoryView = pickerToolbar;
}

不要忘记 pickerCancel pickerDone 函数,当然还有itemSortPicker的委托方法

我希望能解决你的问题。

答案 1 :(得分:1)

问题是UIToolbar完全不在UIPickerView的框架内。在编写代码时,尽管UIToolbarUIPickerView的子视图,但工具栏位于-pickerToolbar.bounds.size.height上方UIPickerView

[pickerToolbar setFrame:CGRectMake(0, -pickerToolbar.bounds.size.height, 320, 44)];

因此,您无法与其按钮进行互动,因为它悬挂在超级视图框架的边缘。例如,如果您更改了pickerToolbar框架:

[pickerToolbar setFrame:CGRectMake(0, 0, 320, 44)];

你应该能够与它互动。

如果您需要-pickerToolbar.bounds.size.height以上的特定展示位置UIPickerView,我建议您不要将其作为子视图添加到UIPickerView,并将其作为子视图正确放置到UIPickerView取而代之的是超级视图。通常,如果您需要与子视图进行交互,则需要完全在其超级视图的框架内。

编辑:在这种情况下,如果您要将工具栏添加到超级视图中,请执行以下操作:

[self.view addSubview:pickerToolbar];

我建议尝试使用此框架来保持原始位置:

[pickerToolbar setFrame:CGRectMake(self.itemSortPicker.frame.origin.x, self.itemSortPicker.frame.origin.y - pickerToolbar.bounds.size.height, 320, 44)];

(为了计算这个新框架,我已将UIPicker' sx和y原点值添加到框架&#s;和x以调整视图中的更改&#39 ; s坐标系。)