I have created a custom tableviewcell through interface builder with a label and a button. In the .h and .m file I have made outlets and actions for the button which I have connected.
This cell is added to a tableview controlled by a uiviewcontroller class. However my problem is that when I tap the button, the button is not activated, however I am pushed on to the detailed view belonging to the cell. It seems like the button is behind the cell.
Any suggestions for what I have forgotten or should change?
I have created a button programatically and added as subview to the cell instead and this works, however this gives me another problem as the button is added everytime a cell is loaded.
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectMake(200.0f, 5.0f, 100.0f, 40.0f)];
//Set image
UIImage *img = [UIImage imageNamed:#"myPackage.png"];
[button setImage:img forState:UIControlStateNormal];
[img release];
[button addTarget:self action:#selector(myPackagePushed:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:button];
I would really like to use the interface builder approach - any suggestions?
Regards
try
[cell.contentView addSubview:button];
also
UIImage *img = [UIImage imageNamed:#"myPackage.png"]; here you are not allocating memory so no need for
[img release]; because there is a possibility of crash
The button should fire the IBAction method that you have set in IB. Have you checked by inserting an NSLog or by setting a breakpoint if the method is entered?
Is the button set to be initially activated in IB?
The crux with the IB construction is that the IBOutlet gets its value when the cell is loaded from the xib. The IBOutlet will then be the button in the cell that the tableview happend to load last.
WHen you add the button programatically, you might need to have the previous added button to removeFromSuperview before you add a button again to that cell.
In my case, the problem was that the button was a subview of the background view.
Buttons assigned to the background view apparently can't respond to touches.
Related
I have a UIViewController which I want to display a UIView that renders as a menu. This menu will have several buttons on it. I wanted to reuse this menu a few different places in my app so I figured I would create a class called ViewFactory that has a method that returns a UIView with these buttons.
On my ViewController I call this method and get the returned UIView and add it as a subview.
This works just fine. I can see the view and all its buttons, however, the buttons do not respond to any touch events. Not sure why this is the case and curious to know what I am doing wrong.
Here is my code for the ViewFactoryClass:
- (UIView *) addCloseRow
{
// UIView container for everything else.
UIView *navRow = [[UIView alloc] initWithFrame:CGRectMake(0,225,350,45)];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.userInteractionEnabled = YES;
[navRow addSubview:button];
[button addTarget:self action:#selector(closeButtonTouchDownEvent) forControlEvents: UIControlEventTouchDown];
navRow.userInteractionEnabled = YES;
return navRow;
}
In my main NavigationController class here is how I am calling and getting the UIView:
ViewFactory *factory = [[ViewFactory alloc] init];
[self.navigationController.navigationBar addSubview:[factory MainNavigationUIView]];
Again, the UIView shows up but the buttons never respond to anything.
You added the button with target and selector for ViewFactoryClass
And now you are creating instance and trying to call an action from ViewFactory class.
You can change the method to something like this:
- (UIView *) addCloseRow : (id)object {
...
[button addTarget:[object class] action:#selector(closeButtonTouchDownEvent) forControlEvents: UIControlEventTouchDown];
...
}
I've requirement to create dynamically some controllers. In the image provided here I've programmatically added an UITextField (name), which hides UITableView.
UITableView is hidden by default. When user touches the UIButton above it, UITableVIew gets appear.
My question when UITableView gets appear, how can I make UITableView top of all other controls?
you will have to change the sequence.
Add UItextfield first
[self.view addSubview:yourTextField];
and add tableview and other views after that line of code so that they appear above it.
[self.view addSubview:yourTableView];
Try
[self.view bringSubviewToFront: yourTableView];
I think this will work fine so please implement this one.
UITextField *txtFld = [[UITextField alloc] initWithFrame:CGRectMake(65, 300, 200, 30)];
txtFld.backgroundColor = [UIColor clearColor];
// Border Style None
[txtFld setBorderStyle:UITextBorderStyleNone];
[txtFld setPlaceholder:#"Name"];
[self.view addSubview:txtFld];
I've been following this example in everything to create a UIWindow on top of the statusBar.
My UIWindow gets displayed on top of the statusBar and all is fine, but the actual view of the app (the one with the button) doesn't respond to my actions:
I'm using Storyboards and iOS6.
Here's my code for creating a statusBar overlay:
- (void)viewDidAppear:(BOOL)animated {
UIWindow *overlayWindow = [[ACStatusBarOverlayWindow alloc] initWithFrame:CGRectZero];
AppDelegate *app = [[UIApplication sharedApplication] delegate];
overlayWindow.rootViewController = app.window.rootViewController;
app.window = overlayWindow;
[overlayWindow makeKeyAndVisible];
}
The view under the statusBar does not respond and I can't tap on the UIButton. Is it possible to somehow make the UIWindow with the interface of my app accept the touches ignoring the ACStatusBarOverlayWindow? How can that be done?
Usually if a button does not respond to a touch it's because the button is outside of the bounds of it's parent's UIView.
Your code does not seem to be the appropriate approach to the problem you're trying to solve. If you just need your window to have a status bar, or you just need to add a button to you current view, the way you're doing it is probably incorrect.
Personally I've never seen anyone instantiate a UIWindow in a viewDidAppear, since the app comes with it's own UIWindow. You should be using a UIView and adding your overlay to it.
As a side note if you were to do it the way you're attempting to, then your window would at least need a frame. So initWithFrame:CGRectZero would be initWithFrame:CGRectMake(0,0,320,480) or something along those lines.
A better way to approach the problem is to instantiate a UIViewController and set it as your rootViewController. Or simply add your button to the current viewController's view.
- (void)viewDidLoad {
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:#selector(myMethod:)
forControlEvents:UIControlEventTouchUpInside];
[button setTitle:#"Tap Me" forState:UIControlStateNormal];
[button sizeToFit];
[self.view addSubview:button];
}
This is what I do
Setup a view with UIViewController Subclass
Go to IB, and pull a view into the iphone
Pull a navigation bar over the UIView
Pull a tableview on the space below.
Everything appears well when I am running the interface builder.
But these things do not work.
If I add a button on top of the Navigation Bar, the button appears but IBAction is not called. The button seems to be at a level beneath the Navigation Bar (somewhere else)
If I add a footer view for the tableView, it does not appear. I believe the UIView is again masking it or something...
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
UIView *viewForFooter = [[UIView alloc] init ];
UIButton *footerButton = [[UIButton alloc] initWithFrame:CGRectMake(200, 40, 100, 40)];
footerButton.titleLabel.text = #"Forgot Login";
//self.forgotLogin = footerButton;
[viewForFooter addSubview:footerButton];
return viewForFooter;
}
WHY ?
1) No reaction to click: Try to add the action in code. If it works, something was wrong with connecting up the IBAction.
[self.barButton setTarget:self];
[self.barButton setAction:#selector(IBActionHandler:)];
As a backup, you can also try to add the button in code:
UIBarButtonItem *barButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:self
selector:#selector(IBActionHandler:)];
self.navigationItem.rightBarButtonItem = barButton;
[barButton release];
2) Invisible button - I think your button is a custom UIButton, so it is transparent. The title is not visible because you have to set it like this:
[footerButton setTitle:#"Forgot Login" forState:UIControlStateNormal];
If you want a regular rounded angle button you have to set the frame after creating it.
UIButton *footerButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
// Careful, this button is autoreleased.
[footerButton setFrame:CGRectMake(200, 40, 100, 40)];
BTW, something might also be wrong with your CGRects. The footerView should perhaps also be properly initialized with a frame, and you might have to implement
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
as well.
PS: To go with a standard UITableViewController might actually be a good idea.
Happy coding!
I've added a button to a tableview cell, but I'm having two problems with it:
1) I've invoked a setTarget:action: method on the button, but the action: method never gets called:
[playButton addTarget:self action:#selector(playButtonPressed:)
forControlEvents:UIControlEventTouchUpInside];
2) the cell itself ends up getting selected on the touch, invoking didSelectRowAtIndexPath, which I don't want to happen.
I've verified that the button is at least seeing the touch by setting its showsTouchWhenHighlighted property and seeing it "glow" when pressed. But for whatever reason it looks like the touch event is then getting passed up to its cell superview instead of being handled by the button itself.
I've tried adding the button directly to the cell's contentView, as well as to other cell subviews, with no effect. I've also made sure that the button's superview(s) were all touch-enabled.
Any thoughts?
Howard
There are two ways to do that, the simplest is to add the button to the cell with IB, set a tag number to the button and when you are in the cellForRowAtIndexPath method updating the cell use something like:
UIButton *myButton = (UIButton *)[cellYouAreUpdating viewWithTag:-1969];
being -1969 the tag number you previously set in IB and cellYouAreUpdating the cell reference. After that you will have a reference to the button in the cell and you could set the add target with the proper selector.
Hope this helps.
My be your button Y axis OR Y axis + height is Higher than its super View (Super view may be cell or contentview of cell) check the heght of superview
-->For your first problem,use this code
UIButton *imgV1 = [UIButton buttonWithType:UIButtonTypeCustom];
[imgV1 setFrame:CGRectMake(0, 0, 320, 53)];
[imgV1 setBackgroundColor:[UIColor clearColor]];
[imgV1 setImage:[UIImage imageNamed:#"expired.png"] forState:UIControlStateNormal];
[imgV1 addTarget:self action:#selector(show_product_details:) forControlEvents:UIControlEventTouchUpInside];
-->For your second problem use this in
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
}