Custom UIButton No Longer Works After Changing UIView to UIViewController - ios

This code worked in a UIView container, but after changing that container to a UIViewController, the code no longer works:
-(void)viewDidLoad
{
[super viewDidLoad];
[self.view setUserInteractionEnabled:YES];
// set button
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(60, 30, 220, 220);
[button setImage:self.obj.img forState:UIControlStateNormal];
[button addTarget:self action:#selector(selected:) forControlEvents:UIControlEventTouchUpInside];
[button setUserInteractionEnabled:YES];
[self.view addSubview:button];
}
-(void) selected:(UIButton *) button // also tried (id) sender
{
NSLog(#“Button Selected“);
}
These containers are created programmatically (dynamically), from web services, so I cannot use Storyboard. The reason for changing to a UIViewController is that I need a segue to another screen. Also, these containers are placed in a UIScrollView.
Everything else is working properly, except the selector.
Setting the userInteraction wasn't necessary earlier, but I've gone ahead and tried that in different configurations. I've added this to the scrollview, the viewController, and the button, just for good measure.
I even tried some online tutorials for programmatically creating UIButtons in UIViewControllers and was able to do that just fine. Not sure what the issue is here. Thanks.

Related

UIButton click event crash after adding the viewcontroller's view as a sub view

I want to add several ViewController's views as sub views of my another viewcontroller. So I have done like this inside my parent viewController.
-(void)MakeDisplayArea
{
vwDisplayArea=[[UIView alloc] initWithFrame:CGRectMake(0, 0, w, h-scrolTab.frame.size.height)];
[self.view addSubview:vwDisplayArea];
}
-(void)ProfClick :(id)sender
{
[self MakeDisplayArea];
ProfileViewController *profviewcontroller=[[ProfileViewController alloc] initWithNibName:#"ProfileViewController" bundle:nil];
profviewcontroller.view.frame=vwDisplayArea.frame;
[profviewcontroller.view setBackgroundColor:[UIColor clearColor]];
[vwDisplayArea addSubview:profviewcontroller.view];
}
Then inside the profViewcontroller ViewDidLoad methods I am generating a button and set the target like this.
UIButton *btn=[[UIButton alloc] initWithFrame:CGRectMake(10, 60, 100, 30)];
[btn setTitle:#"Button Test" forState:UIControlStateNormal];
[self.view addSubview:btn];
[btn addTarget:self action:#selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
-(IBAction)btnClick :(id)sender
{
NSLog(#"button clicked-------");
}
I can see the button is appearing on that profViewcontroller that I loaded as a sub view, but when I click the button the app is crashing. What is the correct way add that viewcontroller as a subview on my first viewcontroller. Please help me.
Thanks
Like maddy said you need child view controllers to add working subviews.
Look here: Creating Custom Container View Controllers
But working with them is not easy. Ask yourself if you really need to implement it. For the most cases you don't need it.

Displaying UIButton programmatically

I'm trying to display a button to the view programmatically with this code:
- (void)viewDidLoad {
_detailButton = [UIButton buttonWithType:UIButtonTypeCustom];
_detailButton.frame = CGRectMake(23, 294, 72, 37);
[_detailButton setTitle:#"UIButton!" forState:UIControlStateNormal];
[_detailButton addTarget:self
action:#selector(showPop:)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:_detailButton];
}
I have the button declared in the .h file like this:
#property (strong, nonatomic) UIButton *detailButton;
The code does not throw any errors or warnings, but when I run there is no button displayed.
I check your code in my project its working fine i think in your case there are some other view that cover this button so check that your code has no problem at all.
Your button probably covered by another view. Just make sure its on top of other views.
In viewWillAppear add the following line of code:
[self.view bringSubviewToFront:self.detailButton];
If your button's type is of UIButtonTypeCustom, the button's default title color is white, so you can't see it if your view's background color is white.
[_detailButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

UIView/UIButton coordinates flipped

I have a coreplot project. The class of the view in viewcontroller has been changed to CPTGraphHostingView.
After creating the graph (this works fine),
I am adding a button as a subview , this launches a popover
the button shown is inverted. I have the same control in different view controllers and it works fine. The Popover also appears fine.
Here is the code that creates and adds the UIButton
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:#selector(monthSelection)
forControlEvents:UIControlEventTouchDown];
[button setTitle:#"Change Month" forState:UIControlStateNormal];
button.frame = CGRectMake(330, 650, 111, 40);
[self.view addSubview:button];
[here is a screenshot of whats happening. UIButton is suppose to show somewhere below the popover. What am i missing.
Do not add subviews to the graph hosting view. Instead, make other views siblings of the hosting view, i.e., add them as subviews of the same superview.

UIButton in UITableViewCell not responding in iOS 5x but responding in iOS 6

I have a button I'm adding in a UITableViewCell programmatically and it is behaving very strangely.
In iOS 6 it works exactly as expected. In iOS 5x it only responds to touch-down events and not touch-up-inside events. And even the touch down event only fires after you hold down for a second or two.
//Create UIButton
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setBackgroundImage:buttonImage forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonTap) forControlEvents:UIControlEventTouchUpInside];
button.frame = CGRectMake(0, 0, 100, 100);
//Add to UITableViewCell
UITableViewCell *footerCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"FOOTER_CELL"] autorelease];
[footerCell.contentView addSubview:button];
//Also tried this with just [footerCell addSubview:button];
//Fire Action
- (void)buttonTap {
NSLog(#"Tap");
}
Pretty standard issue code. In fact I use this exact code to make buttons all over the place in my app and they all work, except in the table view. There's got to be something I'm not understanding about the structure of the cell.
Got it figured out. Long long ago I had put a UITapGestureRecognizer on the table for another reason. It was interfering with the button's functionality. Thanks guys. (and gals)

Programmatically adding a view with a button

I want to add a view and a button programmatically like follows.
The problem is that the button does not react on clicking. I mean neither does it get highlighted or calls the selector.
The reason is that I want to implement a list row for a recording (a sound file). The list row should be selectable for drill-down and have a play button. So I got a RecordingView subclassing UIView that itself adds the button using a target from the constructor. See code below.
If anyone has a better approach to do this that could also be a solution.
#implementation MyViewController
- (IBAction) myAction {
RecordingView *recordingView = [[RecordingView alloc] initWithFrame:CGRectMake(30, 400, 130, 50)withTarget:self];
[recordingView setUserInteractionEnabled:YES];
[[self view] addSubview:recordingView];
}
#implementation RecordingView
- (id)initWithFrame:(CGRect)frame withTarget:(id) target
{
self = [super initWithFrame:frame];
UIButton *playButton = [[UIButton alloc] initWithFrame:CGRectMake(185, 5, 80, 40)];
[playButton setTitle:#"Play" forState:UIControlStateNormal];
[playButton setTitleColor:[UIColor darkTextColor]forState:UIControlStateNormal];
// creating images here ...
[playButton setBackgroundImage:imGray forState: UIControlStateNormal];
[playButton setBackgroundImage:imRed forState: UIControlStateHighlighted];
[playButton setEnabled:YES];
[playButton setUserInteractionEnabled:YES];
[playButton addTarget: target
action: #selector(buttonClicked:)
forControlEvents: UIControlEventTouchDown];
[self addSubview:playButton];
return self;
}
When I add the button the same way, directly in the view controller .m-file, the button does react on clicking. So there is something about the RecordingView. What do I need to do different here?
Also, are there any better ways to provide the target and selector for the touch event?
You may simply need to set userInteractionEnabled to YES on your RecordingView.
Another problem is that you are creating the RecordingView with a frame width of 130, but you are setting the X-axis origin of playButton to 185. This means playButton is entirely outside of the bounds of its superview. The default value for clipsToBounds is NO, so the button is drawn anyway. But touch events will never reach the button, because they are rejected when the system hit-tests the RecordingView.
This is from the hitTest:withEvent: documentation in UIView Class Reference:
Points that lie outside the receiver’s bounds are never reported as hits, even if they actually lie within one of the receiver’s subviews. This can occur if the current view’s clipsToBounds property is set to NO and the affected subview extends beyond the view’s bounds.
You need to either make RecordingView's frame wider, or move playButton to be inside the bounds of its superview.

Resources