2013-10-29 91 views
1

我正在以編程方式創建UIPickerView,UIToolbar,UIBarButtonItemUIButton。我將自定義選擇器設置爲textView的輸入視圖,除了完成按鈕外,其他所有工作都沒有問題。完成按鈕不能在編程UIBarButtonItem和UIPickerView工作

有沒有人有任何想法是什麼問題?

我使用的代碼是:

// 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]; 
} 

但是完成按鈕不可點擊。

+0

試試我的代碼。使用自定義創建按鈕 – TamilKing

回答

1

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

+0

使用下面的代碼,但它沒有奏效。 [customButton addTarget:self action:@selector(doneClicked)forControlEvents:UIControlEventTouchUpInside]; –

+0

如果您使用@TamilKing示例中的buttonWithType方法,它會起作用嗎?即使用buttonWithType方法和選擇器(doneClicked)創建按鈕,而不使用:。 –

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

嘗試這樣的..

0

更改您的代碼如下: -

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]; 
+0

我改變了你建議的方法,但仍然無法正常工作。我目前的方法是: - (IBAction)doneClicked:(id)sender { [self.txtTeam resignFirstResponder]; } –

+0

已修改您的代碼請修改相同的? –

+0

我的當前代碼是:您的代碼+ NSMutableArray * arr = [NSMutableArray arrayWithObjects:flexibleSpaceLeft,barCustomButton,nil]; [toolbar setItems:arr animated:YES]; [_picker addSubview:toolbar]; self.txtTeam.inputView = _picker;還是行不通。 –

相關問題