0

I have one button and one search field in navigation bar and i added a new button by code from one class for all view.

The problem is: with the button view the other button and the search field added with storyboard are not more touchable and not working.

UIImage * buttonImage = [UIImage imageNamed:@"chat-notify.png"];

button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self
           action:@selector(goChat)
 forControlEvents:UIControlEventTouchDown];
//[button setTitle:@"1" forState:UIControlStateNormal];
button.frame = CGRectMake(280.0, 25.0, 30.0, 30.0);
[button setBackgroundImage:buttonImage forState:UIControlStateNormal];

[self.view addSubview:button];

and then in the controller:

 notificationViewController* notification = [[notificationViewController alloc]init];
//[notification methodA];

[notification makeButtonNotification];

[self.view addSubview:notification.view];

Does anyone know where is my mistake?

Thanks

GingerHead
  • 8,130
  • 15
  • 59
  • 93

1 Answers1

0

Why are you adding the button surely having it there and changing it's hidden state would be easier then you can keep it all in the storyboard

UIControlEventTouchUpInside

is what you should use not "UIControlEventTouchDown"

TouchUpInside is usually the event where you trigger the action from a button. It's when the user is lifting up from the button after pressing it. You don't want to trigger the action on Touch Down as the use may move his finger off the button to cancel the press, which is standard and expected behaviour on iOS

UPDATE: Base on your comment to this answer perhaps you need to add the button to a specific point in your view heir achy. As in your storyboard views will be added as they are listed in that stroyboard starting from the top down, with the one on the bottom being the top view.

Try obviously making sure that views that you want to receive touches are not directly on top of each and that they do not overlap. Also try adding the programatically generated button above or below a specific view, rather than just making it the top subview in your main view which by the sounds of it isn't what you want.

As you're adding a button like this

[self.view addSubview:button];

it may be better to do it more like this

[self.view insertSubview:button aboveSubview:someOtherView]

To do this you'll probably need to hook up your "someOtherView" to an IBOutlet so you can reference it in your code. As it looks like you're most likely sticking your button over everything else, when you need to be more precise.

AppHandwerker
  • 1,758
  • 12
  • 22
  • uhm.. i don't understand... what about UIControlEventNormal? and i need have one button by code because i have to set number notification, and that button is in all views and the other button us JASidePannel and doesn't wont work programmatically... – Gabriele Carbonai Feb 20 '14 at 15:44
  • Sorry I don't understand your issue perhaps show more code. As using the same button in multiple view isn't surely what you're trying to do. – AppHandwerker Feb 20 '14 at 15:46
  • Thanks, i didn't know it. by the way, is not that the problem.. is like when i add the button view from code, this covers the others object... – Gabriele Carbonai Feb 20 '14 at 15:50
  • I've updated my answer to hopefully give you an idea of how to solve you problem. – AppHandwerker Feb 21 '14 at 10:16