簡體   English   中英

按下時不會激活工具欄按鈕

[英]Toolbar button wont get activated when pressed

我以編程方式向工具欄添加了一個按鈕,但是它不起作用。 我的代碼有什么問題? 它甚至沒有壓力。 就像工具欄上的圖片一樣。

-(void)viewWillAppear:(BOOL)animated{

   //Toolbar send button
   UIView *buttonContainer1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 44)];
   buttonContainer1.backgroundColor = [UIColor clearColor];
   UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
   [button1 setBackgroundImage:[UIImage imageNamed:@"send.png"] forState:UIControlStateNormal];
   const CGSize button1Size = [button1 sizeThatFits:CGSizeZero];
   [button1 setFrame:CGRectMake([UIScreen mainScreen].bounds.size.width-40, 27, button1Size.width, button1Size.height)];
   [button1 addTarget:self action:@selector(button1Action:) forControlEvents:UIControlEventTouchUpInside];
   [button1 setShowsTouchWhenHighlighted:YES];
   [buttonContainer1 addSubview:button1];

}

-(IBAction)button1Action:(id)sender{
   textField.textColor = [UIColor lightGrayColor];
   textField.text = @"the message was sent";
   [textField resignFirstResponder];

}

將以下代碼用於文本欄的工具欄。 我一直在我的代碼中使用它

 UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button.layer setMasksToBounds:YES];
button.frame=CGRectMake(0.0, 100.0, 60.0, 30.0);
[button addTarget:self action:@selector(button1Action:)  forControlEvents:UIControlEventTouchUpInside];

UIToolbar* keyboardDoneButtonView = [[UIToolbar alloc] init];
keyboardDoneButtonView.backgroundColor = [UIColor blackColor];
[keyboardDoneButtonView sizeToFit];
UIBarButtonItem* doneButton = [[UIBarButtonItem alloc] initWithCustomView:button];
[keyboardDoneButtonView setItems:[NSArray arrayWithObjects:doneButton, nil]];
textfield.inputAccessoryView = keyboardDoneButtonView;

您在viewWillAppear最后忘了一行重要的代碼:

[self.view addSubview:buttonContainer1];

僅供參考-此代碼應位於viewDidLoad ,而不是viewWillAppear viewWillAppear可能會被多次調用,因此您最終將添加多個按鈕。

而且與您的代碼無關的工具欄也沒有任何關系。

暫無
暫無

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

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