簡體   English   中英

Done Button不適用於程序化UIBarButtonItem和UIPickerView

[英]Done Button not working at programmatic UIBarButtonItem and UIPickerView

我正在以編程方式創建UIPickerViewUIToolbarUIBarButtonItemUIButton 我將自定義選擇器設置為textView的inputView,除了完成按鈕之外,一切都可以正常工作。

有誰知道問題是什么?

我使用的代碼是:

// initialize picker
CGRect cgRect =[[UIScreen mainScreen] bounds];
CGSize cgSize = cgRect.size;

_picker = [[UIPickerView alloc] init];
_picker.frame=CGRectMake(0, 0, cgSize.width, cgSize.height);
_picker.showsSelectionIndicator = YES;
_picker.delegate = self;

// toolbar of picker
UIToolbar* toolbar = [[UIToolbar alloc] init];
toolbar.frame=CGRectMake(0, 0, cgSize.width, 35);
toolbar.barStyle = UIBarStyleBlackTranslucent;

UIBarButtonItem *flexibleSpaceLeft = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];

UIButton *customButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 60, 33)];
[customButton setTitle:@"Done" forState:UIControlStateNormal];
[customButton addTarget:self action:@selector(doneClicked:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *barCustomButton =[[UIBarButtonItem alloc] initWithCustomView:customButton];

NSMutableArray * arr = [NSMutableArray arrayWithObjects:flexibleSpaceLeft, barCustomButton, nil];
[toolbar setItems:arr animated:YES];
self.txtTeam.inputView = _picker;
[_picker addSubview:toolbar];

doneClicked方法是;

    -(void)doneClicked
{
    NSLog(@"Done button clicked.");
    [self.txtTeam resignFirstResponder];
}

但是“完成”按鈕不可點擊。

UIButton *customButton =  [UIButton buttonWithType:UIButtonTypeCustom];
[customButton setFrame:CGRectMake(0, 0, 30, 60)];
[customButton setTitle:@"Done" forState:UIControlStateNormal];
[customButton addTarget:self action:@selector(doneClicked:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *barCustomButton =[[UIBarButtonItem alloc] initWithCustomView:customButton];

嘗試這樣..

您已將按鈕的選擇器設置為調用doneClicked:但該方法稱為doneClicked。 從選擇器中的方法名稱的末尾刪除:它應該工作。

將您的代碼更改為: -

UIButton *customButton = [UIButton buttonWithType:UIButtonTypeCustom];
customButton.frame = CGRectMake(0, 0, 60, 33);
[customButton addTarget:self action:@selector(doneClicked) forControlEvents:UIControlEventTouchUpInside];
customButton.showsTouchWhenHighlighted = YES;
[customButton setTitle:@"Done" forState:UIControlStateNormal];
customButton.imageEdgeInsets = UIEdgeInsetsMake(10, 10, 10, 10);
UIBarButtonItem *barCustomButton =[[UIBarButtonItem alloc] initWithCustomView:customButton];

只需在下面注釋您的代碼並在上面進行修改

   // UIButton *customButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 60, 33)];
   // [customButton setTitle:@"Done" forState:UIControlStateNormal];
    //[customButton addTarget:self action:@selector(doneClicked:) forControlEvents:UIControlEventTouchUpInside];
   // UIBarButtonItem *barCustomButton =[[UIBarButtonItem alloc] initWithCustomView:customButton];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM