an UIButton in an UIPickerView doesn't respond to click event - iphone

i add an UIButton to an UIPicker view bu this code
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
UIView *returnedView=(id)view;
if (!returnedView) {
returnedView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, [pickerView rowSizeForComponent:component].width, 44)];
}
returnedView.userInteractionEnabled=YES;
UILabel *retval=[[[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f,50, 44)] autorelease];
retval.text = [[_figureArrayPicker objectAtIndex:row] valueForKey:#"FIGPic"];
UIButton *button=[self pieChartOfFigureWithCmm:row];
[button setFrame:CGRectMake(50, 0, 50, 44)];
[button addTarget:self action:#selector(defectPressed:)
forControlEvents:UIControlEventTouchUpInside];
[returnedView addSubview:button];
}
[returnedView addSubview:retval];
return returnedView;
}
but when i click on an uibutton this doesn't respond.
where was my mistake?
Thks.

Just try explicitly changes userInteractionEnabled=YES for button also, may that can be a case here.

Allocate memory to your button.

Related

Button action not call in pickerview delegate method viewForRow in iOS7?

In my app I have added custom view on picker which has a lable and button with a target method but when I tap on button method not call.
Here is my code-
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
UIView *customView = [[UIView alloc]initWithFrame:CGRectMake(0,0,320,35)];
UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(2,1,280,33)];
[lbl setText:[arrPicker objectAtIndex:row]];
[lbl setTextAlignment:NSTextAlignmentCenter];
[lbl setBackgroundColor:[UIColor blueColor]];
[customView addSubview:lbl];
UIButton *btnPicker = [UIButton buttonWithType:UIButtonTypeCustom];
[btnPicker setFrame:CGRectMake(285,3,29,29)];
[btnPicker setBackgroundImage:[UIImage imageNamed:#"btn_radio_uncheked.png"] forState:UIControlStateNormal];
[customView setUserInteractionEnabled:YES];
[btnPicker setUserInteractionEnabled:YES];
[btnPicker setTag:row];
[btnPicker addTarget:self action:#selector(btnCheckPicker:) forControlEvents:UIControlEventTouchUpInside];
[customView addSubview:btnPicker];
return customView;
}
- (void)btnCheckPicker:(UIButton *)btn{
NSLog(#"PickerButtonAction");
}

Blue coloured Footer in UITableView for an iPhone Native App in Objective C

I am developing an iPhone App using X-Code in objective C. I want to add a blue coloured footer (same as header) at the bottom of the table.
Can anyone help me in suggesting How this can be achieved. Also How to add a button in this footer.
Thanks in advance.
Hope this will help you
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
UIView *viewFotter = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
[viewFotter setBackgroundColor:[UIColor blueColor]];
return viewFotter;
}
But the thing is, u can't resize the button.Because it's doesn't worked for me.
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
return 40;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];
footerView.backgroundColor =[UIColor blueColor];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setTitle:#"BUTTON" forState:UIControlStateNormal];
btn.frame = CGRectMake(100, 5, 100, 30 );
[btn addTarget:self action:#selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
[footerView addSubview:btn];
return footerView;
}
-(void) btnClicked:(id) sender{
//ur code
}
paste this code in your .m file also you can add image in your footerview
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
return 44;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
UIView *viewFotter = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
[viewFotter setBackgroundColor:[UIColor blueColor]];
UIButton *GoBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[GoBtn setTitle:#"GO" forState:UIControlStateNormal];
GoBtn.frame = CGRectMake(85, 7, 150, 39 );
[GoBtn addTarget:self action:#selector(GOButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
[viewFotter addSubview:GoBtn];
return viewFotter;
}
You can add the button by just dragging the UIButton to the footer of the UITableView in xib.But you can't resize the button as it will automatically assigns to the table view's width.
But I don't understand what you are saying regarding footer color.Can you pls elaborate it.
You can create a custom view with the components you want (UIButton, UIImageView, etc) and set the required values in
-(UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
Or you could just assign the same view to tableFooterView
tableView.tableFooterView = yourBlueView;

UiButton + UipickerView Action method not calling all devices (below version of iOS 5)

Hi all i have strange problem with UIPickerview + UIButton.. I have
create a project with xcode 4.2. I add UIPickerview with 2 components
in 1st component i add a label and 2nd component i add button, on
button click i have to go other view . every thing is working fine in
simulator and iOS 5 devices but not working in Devices which have
version below iOS 5. My code is here
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
UIView *tempView;
if(component==1)
{
tempView=[[UIView alloc] initWithFrame:CGRectMake(0, 0,60, 50)];
UIButton *_btnYou=[UIButton buttonWithType:UIButtonTypeCustom];
[_btnYou setFrame:CGRectMake(5, 0,55, 50)];
[_btnYou setImage:[UIImage imageNamed:#"icon_i.png"] forState:UIControlStateNormal];
[_btnYou setImage:[UIImage imageNamed:#"icon_u.png"] forState:UIControlStateHighlighted];
[_btnYou addTarget:self action:#selector(GoToRegistrationPage:) forControlEvents:UIControlEventTouchDown];
[tempView addSubview:_btnYou];
[_btnYou setUserInteractionEnabled:YES];
return tempView;
//return _btnYou;
}
else
{
tempView=[[UIView alloc] initWithFrame:CGRectMake(0, 0,168, 50)];
if(!([[dates objectAtIndex:row] isEqualToString:#"Today"]||[[dates objectAtIndex:row] isEqualToString:#"Idag"])){
//add days
UILabel *lab=[[UILabel alloc] initWithFrame:CGRectMake(3, 0,54, 50)];
[lab setBackgroundColor:[UIColor clearColor]];
[lab setTextColor:[UIColor grayColor]];
[lab setTextAlignment:UITextAlignmentRight];
[lab setFont:[UIFont fontWithName:#"Helvetica" size:20]];
[lab setFont:[UIFont boldSystemFontOfSize:20]];
lab.text=[[[dates objectAtIndex:row] componentsSeparatedByString:#","] objectAtIndex:0];
[tempView addSubview:lab];
}
//add month
UILabel *labDate=[[UILabel alloc] initWithFrame:CGRectMake(62, 0,106, 50)];
[labDate setBackgroundColor:[UIColor clearColor]];
[labDate setTextColor:[UIColor blackColor]];
[labDate setTextAlignment:UITextAlignmentLeft];
[labDate setFont:[UIFont fontWithName:#"Helvetica" size:25]];
[labDate setFont:[UIFont boldSystemFontOfSize:25]];
if(([[dates objectAtIndex:row] isEqualToString:#"Today"]||[[dates objectAtIndex:row] isEqualToString:#"Idag"])){
[labDate setTextColor:[UIColor greenColor]];
[labDate setTextAlignment:UITextAlignmentCenter];
labDate.text=[dates objectAtIndex:row];
}else{
labDate.text=[[[dates objectAtIndex:row] componentsSeparatedByString:#","] objectAtIndex:1];
}
[tempView addSubview:labDate];
}
return tempView;
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 2;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if(component == 0)
return [dates count];
else if(component == 1)
return 4;
}
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
if(component == 0)
return 168.0;
else if(component == 1)
return 66.0;
}
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
return 50.0;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
notificationDate=[dates objectAtIndex:[pickerView selectedRowInComponent:0]];
NSLog(#"%#", [dates objectAtIndex:[pickerView selectedRowInComponent:0]]);
}
-(void)GoToRegistrationPage:(id)sender
{
NSLog(#"This is GoToRegistrationPage Button");
}
GoToRegistrationPage metod not calling Devices (version
The problem here is that you aren't retaining your button. The target self in [_btnYou addTarget:self action:#selector(GoToRegistrationPage:) forControlEvents:UIControlEventTouchDown]; is not retained. So when _btnYou get's autoreleased, it's target become nil.
You need to add the button as a retained property of your class, then you can refer to it as self.btnYou and instantiate it like so:
self.btnYou = [UIButton buttonWithType:UIButtonTypeCustom];
In case I am wrong about this retention business, I also noticed that you set the selector for UIControlEventTouchDown. Typically, you fire the actions on UIControlEventTouchUpInside. Don't know if that will make a difference.
Most thinking part is that it is running in all places but not running in lower of iOS 5.0 device.
Apple mentioned about ARC that "ARC is supported in Xcode 4.2 for Mac OS X v10.6 and v10.7 (64-bit applications) and for iOS 4 and iOS 5. Weak references are not supported in Mac OS X v10.6 and iOS 4." please check that also.
I think you are using weak reference for button and it is not supported in iOS 4.0.

cant able to add click event in UIbutton

I am creating custom view which include two labels and One button. I cant get the button click. I am also sending the code which is below.
.h File
#import <UIKit/UIKit.h>
#interface customDeleteButton : UITableViewCell {
IBOutlet UILabel *lbl;
IBOutlet UILabel *lbl1;
}
#property(nonatomic,retain)IBOutlet UILabel *lbl;
#property(nonatomic,retain)IBOutlet UILabel *lbl1;
-(IBAction)btnClick:(id)sender;
#end
.m File
#import "customDeleteButton.h"
#implementation customDeleteButton
#synthesize lbl,lbl1;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code.
lbl = [[UILabel alloc] initWithFrame:CGRectMake(50, 10, 200, 20)];
lbl1 = [[UILabel alloc] initWithFrame:CGRectMake(260, 10, 200, 20)];
[self addSubview:lbl];
[self addSubview:lbl1];
}
return self;
}
- (void)willTransitionToState:(UITableViewCellStateMask)state{
[super willTransitionToState:state];
if ((state & UITableViewCellStateShowingDeleteConfirmationMask) == UITableViewCellStateShowingDeleteConfirmationMask) {
for (UIView *subview in self.subviews) {
if ([NSStringFromClass([subview class]) isEqualToString:#"UITableViewCellDeleteConfirmationControl"]) {
UIButton *btn;
btn=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 64, 33)];
[btn addTarget:subview action:#selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
[btn setImage:[UIImage imageNamed:#"delete.png"] forState:UIControlStateNormal];
btn.userInteractionEnabled=YES;
btn.frame=CGRectMake(0,0,64,33);
//UIImageView *deleteBtn = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 64, 33)];
//[deleteBtn setImage:[UIImage imageNamed:#"delete.png"]];
[[subview.subviews objectAtIndex:0] addSubview:btn];
//[deleteBtn release];
}
}
}
}
-(void)btnClick:(id)sender
{
NSLog(#"HTllo");
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state.
}
- (void)dealloc {
[super dealloc];
}
You need to set the target to your main view not the subviews, when you create it:
[btn addTarget:self action:#selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
And also - do you realize what you're doing in your code?
In a function willTransitionToState you are adding UIButtons to subviews of the button itself?
Is this really what you want to do? Is the function willTransitionToState even called?
Please try setting UIButton Type while initializing and change the target to self.
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(0, 0, 64, 33);
[btn addTarget:self action:#selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
[btn setImage:[UIImage imageNamed:#"delete.png"] forState:UIControlStateNormal];
btn.userInteractionEnabled=YES;
[[subview.subviews objectAtIndex:0] addSubview:btn];
Change the target to self instead of subview.
UIButton *btn;
btn=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 64, 33)];
[btn addTarget:self action:#selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
[btn setImage:[UIImage imageNamed:#"delete.png"] forState:UIControlStateNormal];
btn.userInteractionEnabled=YES;
btn.frame=CGRectMake(0,0,64,33);
//UIImageView *deleteBtn = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 64, 33)];
//[deleteBtn setImage:[UIImage imageNamed:#"delete.png"]];
[[subview.subviews objectAtIndex:0] addSubview:btn];
//[deleteBtn release];
newString = [oldString stringByReplacingOccurrencesOfString:#"-"withString:#" ' "];

UIButtons on tableFooterView not responding to events

I've successfully added some UIButtons to a custom tableFooterView. The problem is that the button events does not get called. Also, the image relative to the UIControlStateHighlighted doesn't come up. Any ideas?
I've tried all I could think of, but I can't find the trick. I'm reporting here the interesting part of the UITableViewController:
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection: (NSInteger)section
{
CGRect footerFrame = CGRectMake(0.0, 0.0, 480, 190);
_footerView = [[UIImageView alloc] initWithFrame: footerFrame];
_footerView.image = [UIImage imageNamed:#"tableviewfooter.png"];
_footerView.contentMode = UIViewContentModeTopLeft;
UIButton *addExam = [self createButtonWithFrame: CGRectMake(10, 30, 460, 58) Target: self Selector: #selector(addNewItem:) Image: #"additem.png" ImagePressed: #"additem_pressed.png"];
[_footerView addSubview:addExam];
return _footerView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection: (NSInteger) section
{
return 190;
}
- (void) addNewItem:(id)sender
{
// This get's never called
NSLog(#"Pressed");
}
- (UIButton*) createButtonWithFrame: (CGRect) frame Target:(id)target Selector:(SEL)selector Image:(NSString *)image ImagePressed:(NSString *)imagePressed
{
UIButton *button = [[UIButton alloc] initWithFrame:frame];
UIImage *newImage = [UIImage imageNamed: image];
[button setBackgroundImage:newImage forState:UIControlStateNormal];
UIImage *newPressedImage = [UIImage imageNamed: imagePressed];
[button setBackgroundImage:newPressedImage forState:UIControlStateHighlighted];
[button addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];
return button;
}
Thanks for your help!
I do not think UIImageView was designed to work well as a container for other views, especially UIButtons. For one, UIImageViews, when created, have their userInteractionsEnabled property set to false by default.
I would just create a simple UIView as your footer view and then add both the UIButton and the UIImageView directly to it.