EXC_BAD_ACCESS on button press for button dynamically added to UIView within UIScrollView - dynamic

OK. It's an iPad app. Within the DetailViewController I have added a UIScrollView through IB and within that UIScrollView I have added a UIView (also added through IB) which holds various dynamically added UITableViews, UILabels and UIButtons.
My problem is that I'm getting errors on the UIButton clicks.
I have defined this method:
- (void)translationSearch:(id)sender {
NSLog(#"in transearch");
[self doSearch];
}
This is how I'm adding the UIButton to the UIView:
UIButton *translationButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
translationButton.frame = CGRectMake(6, 200, 200, 20);
translationButton.backgroundColor = [UIColor clearColor];
[translationButton setTitle:#"testing" forState:UIControlStateNormal];
[translationButton addTarget:self action:#selector(translationSearch:) forControlEvents:UIControlEventTouchUpInside];
[verbView addSubview:translationButton];
Now the button is added to the form without any issue but, when I press it, I am getting an EXC_BAD_ACCESS error. I'm sure it's staring me in the face but I've passed my usual time limit for getting a bug like this fixed so any help would be greatly appreciated. The only thing I can think is the fact that the UIButton is within a UIView which is within a UIScrollView which is within the view controller is somehow causing an issue.
Cheers.

Thanks Maz. I have just done that and I have to admit that my assumption about where the error was coming from was wrong. It was a new bit of code, just not the button method call.
Grant me the "dunce" award for this one.

Related

UIButton not showing up

I am trying to create a UIButton programmatically instead of using the interface builder. I initialize and set the button frame but the button doesn't seem to appear on the view. My code is as follows:
UIButton *showInfoButton = [[UIButton alloc] init];
CGRect buttonFrame = CGRectMake(0,0, 150, 150);
showInfoButton.frame = buttonFrame;
What am I missing? Please bear with me as I am new to iOS.
You forgot to add your UIButton as a subview to the UIView you want it to display on. Since you are building UI programmatically, you need to manually add the button as a subview to the container view.
You can do so by using this line of code:
[self.view addSubview:showInfoButton];
The button might not still appear as you are making a custom button which has a transparent background colour. In order to see the button, you can set the UIButton's backgroundColor property as follows:
[showInfoButton setBackgroundColor:[UIColor blackColor]];

Custom UIButton background image not appearing until tapped

In app my, if you tap on a certain area a UIPopoverController appears with UIButtons that perform certain tasks when clicked. The UIButtons (called CableDisconnectButton) are a subclassed UIButton so I could add two additional properties to them. I also add UILabels to go over the buttons
However, the background images of the buttons are invisible or don't appear until I tap on the screen somewhere. The UIlabels show up fine, but not the buttons. It can be a tap on the UIPopoverController or anywhere else on the screen. Once I've tapped that first time, the buttons will be there until the app is closed. So, this only happens right after launch and up until I first open that UIPopover. I tap plenty of times before opening the popover.
The functionality of the buttons and everything else works fine, but the background images are hidden on that first launch and I have no idea why.
Here's how I create the buttons and UILabel:
//create custom button
CableDisconnectButton *removeConnectionButton = [CableDisconnectButton buttonWithType:UIButtonTypeCustom];
removeConnectionButton.frame = CGRectMake(x, y, 190, 80);
removeConnectionButton.backgroundColor = [UIColor clearColor];
[removeConnectionButton setBackgroundImage:[UIImage imageNamed:#"images/cable_disconnect_button.png"] forState:UIControlStateNormal];
[removeConnectionButton setBackgroundImage:[UIImage imageNamed:#"images/cable_disconnect_button_over.png"] forState:UIControlStateHighlighted];
//set input and output jacks to button properties
removeConnectionButton.inputJack = inputJack;
removeConnectionButton.outputJack = self.outputJackView;
//add action to button
[removeConnectionButton addTarget:self action:#selector(removeConnectionButtonTarget:) forControlEvents:UIControlEventTouchUpInside];
//create label for output
UILabel *outputConnectionLabel = [[UILabel alloc] initWithFrame:CGRectMake(x+18, y+5, 180, 22)];
outputConnectionLabel.backgroundColor = [UIColor clearColor];
outputConnectionLabel.textColor = [UIColor whiteColor];
outputConnectionLabel.text = self.outputJackView.jackDisplayName;
outputConnectionLabel.font = [UIFont systemFontOfSize:16];
//add subviews
[self addSubview:removeConnectionButton];
[self addSubview:outputConnectionLabel];
I've tried to add a regular, non-custom UIButton and it appears without the tap. I suspect it may have something to do with the subclassed UIButton, but I'm not sure why. The extra properties added to the UIButton are strings that are crucial to the functionality of the and can't be omitted.
After beating my head off the desk for days, I ran into the "Clean Build Folder" option. I've cleaned the project plenty of times, but wasn't aware of "Clean Build Folder". To execute this, simple hold the Option key, click Product from the menu and select Clean Build Folder.
So, if your app isn't behaving the way it should and it makes no sense AT ALL, try this.

UIButton not clickable after UITableView scrolled

I have a UITableView which is populated with some cells. I have created a UIButton using the following snippet, it is placed next to one of the section headers.
UIButton *addButton = [UIButton buttonWithType:UIButtonTypeCustom];
[addButton addTarget:self action:#selector(addButtonPressed) forControlEvents:UIControlEventTouchUpInside];
[addButton setBackgroundImage:[UIImage imageNamed:#"add.png"] forState:UIControlStateNormal];
addButton.backgroundColor = [UIColor clearColor];
addButton.frame = CGRectMake(270, 150, 29, 29);
The button is placed and works correctly. However, after the view is scrolled (even slightly - like 1 pixel), the button works once and then ceases to respond. When it fails to respond the action for when it is clicked is not triggered and the button doesn't give the 'depressed' shadow. The rest of the application runs as normal and it does not crash.
This seems odd because after I scroll the button is clickable once more before it stops working. The button is used to insert rows into the table, so after it is pressed there is an extra row, possibly this is breaking the bounds or something?
Button pressed function:
- (void)addButtonPressed {
self.addClientTable.editing = YES;
// First figure out how many sections there are
NSInteger lastSectionIndex = [self numberOfSectionsInTableView:self.addClientTable] - 1;
// Then grab the number of rows in the last section
NSInteger lastRowIndex = [self.addClientTable numberOfRowsInSection:lastSectionIndex];
[self.addClientTable insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:lastRowIndex inSection:lastSectionIndex]] withRowAnimation:UITableViewRowAnimationRight];
self.addClientTable.editing = NO;
}
Where addClientTable is the UITableView.
What could cause a UIButton to stop responding to clicks and where in my scenario would this be caused by?
I am almost sure that your problem is that your button is out of it superview, and you are not using the clip subviews option in your view that contains the button, or in one of it superviews.
Set to true all the views property clip subviews and see if it appears your button. (We expect that the button disappear)
If you provide more code I can try to help you to solve this problem.
-
Reading again your question, another probable problem to it is that you have one view in front of your button. You can test it changing the background of your view, or something like that.

Creating/Displaying Interface Objects with Code for the iPhone [Objective-C]

I'm currently designing an app that calls for dynamically creating objects and then displaying them in a ViewController. My problem is wading through the documentation to try and create the objects I need. I've done some searching and even tried to just sit down and figure it out, but alas, I've got nothing to show for it. I already know how to dynamically create the ViewController but I can't seem to get any objects in it.
Does anyone know how I could go about creating objects (Say a button and a slider) dynamically with Objective-C in XCode?
Creating a button in code is simple
- (void)addButton {
// Create button
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
// Set the frame (location and size)
[button setFrame:CGRectMake(0, 0, 40, 30)];
// Set what happens when you touch the button
[button addTarget:self action:#selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];
// Set button text
[button.titleLabel setText:#"Button"];
// Add button to view
[self.view addSubview:button];
}
A lot of interface elements follow a similar patter- alloc and init the object, set the frame and add it to a view.

Button not working in UITableViewCell subclass when it is a subview of a view inside the UITableViewCell

I have a button in a tableView Cell that I need to respond to touch events. Normally this would easily be solved by
[cell.playButton addTarget:self action:#selector(playButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
inside of -tableView:cellForRowAtIndexPath:
The problem I am having is that my button is inside of a custom UITableViewCell subclass and is also the subview of a view that I am creating inside of that class..
for example:
UIView *view = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
self.theView = view;
[self addSubview:view];
[view release];
UIButton *playButton = [UIButton buttonWithType:UIButtonTypeCustom];
playButton.frame = CGRectMake(5, 5, 100, 30);
[playButton setImage:[UIImage imageNamed:#"button_playvid.png"] forState:UIControlStateNormal];
[playButton setImage:[UIImage imageNamed:#"button_playvid_active.png"] forState:UIControlStateHighlighted];
self.playButton = playButton;
[self.theView addSubview:playButton];
When the button is a subview of a view that I create within the custom UITableViewCell then the cell is highlighted instead of the button being pressed so the target that I setup in -tableView:cellForRowAtIndexPath is never called. Not sure why.. any help?
Thanks,
Joel
PS. I realize that there probably isn't a practical reason to create a background view exactly like this since there already is one. This is just example code that I'm using to explain a problem I'm having.. :D
Thanks for Looking!
The UIView you are adding is actually a UIImageView. From the docs:
New image view objects are configured to disregard user events by default. If you want to handle events in a custom subclass of UIImageView, you must explicitly change the value of the userInteractionEnabled property to YES after initializing the object.
I think that either userInteractionEnabled cascades to subviews or the superview will not pass the touch to its subviews in this case, so your button will not be receiving touches. Set your image view to have .userInteractionEnabled = YES and you should be fine.
I had the same problem and tried EVERYTHING. The thing that finally did it was implementing the method
(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
Just return whatever the height is of your custom cell (look in the NIB file)
Recently I've ran into such issue with legacy code. The reason was that earlier we used to add subviews to custom UITableViewCell instance itself and now we have to add to contentView of the cell instance.
I fixed it by changing:
addSubview(increaseButton)
to
contentView.addSubview(increaseButton)
Of course, isUserInteractionEnabled should be true.
Instead of button, you could try the same with an image. You could define which method to be called when the image is clicked.