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

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");
}

Related

Created Table Cell With UIView as subview Not Displaying in iOS7

I have created UIView on top of the UITable Cell but its not displaying in iOS7 which is working fine in iOS6. Kindly provide me the solution.
We are adding the More button # the end of the table.
Added code for the same:
UIView *footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 768, 40)];
footerView.backgroundColor = [UIColor grayColor];
UIButton *btnLoadMore = [UIButton buttonWithType:UIButtonTypeCustom];
btnLoadMore.frame = CGRectMake(-10, 0, 768, 30);
btnLoadMore.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleWidth;
[btnLoadMore setTitle:#"Sample" forState:UIControlStateNormal];
[btnLoadMore setBackgroundColor:[UIColor redColor]];
[btnLoadMore setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[btnLoadMore addTarget:self action:#selector(loadMore) forControlEvents:UIControlEventTouchUpInside];
btnLoadMore.userInteractionEnabled=YES;
[btnLoadMore.titleLabel setFont:[UIFont fontWithName:#"Helvetica" size:17.0]];
[footerView addSubview:btnLoadMore];
[footerView setHidden:YES];
[footerView setTag:999];
[cell addSubview: footerView];
for (id subView in [cell subviews]) {
[subView setHidden:NO];
}
UIView *lastRow = [cell viewWithTag:999];
[lastRow setHidden:YES];
if(indexPath.section == [arSearch count] && isLoadMore){
for (id subView in [cell subviews]) {
[subView setHidden:YES];
}
cell.backgroundView = nil;
[lastRow setHidden:NO];
Check your code. Why you have written following line :
[footerView setHidden:YES];
I think you are hiding view from cell.
Instead of
[cell addSubview: footerView];
use
[cell.contentView addSubview: footerView];
Also remove
[footerView setHidden:YES];
For loadmore, I always do below.
UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 65)];
v.backgroundColor = [UIColor clearColor];
int mySiz = 0;
// keep counter how many times load more is pressed.. initial is 0 (this is like index)
mySiz = [startNumberLabel.text intValue]+1;
// i have 15 bcz my index size is 15.
if ([feeds count]>=(15*mySiz)) {
NSLog(#"showing button...");
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectMake(10, 10, 296, 45)];
[button setBackgroundImage:[UIImage imageNamed:localize(#"loadmore")] forState:UIControlStateNormal];
[button addTarget:self action:#selector(loadMoreData:) forControlEvents:UIControlEventTouchUpInside];
[v addSubview:button];
mainTableView.tableFooterView = v;
} else {
mainTableView.tableFooterView = nil;
}
}
[mainTableView reloadData];

Keep a UIButton selected for a fraction of time in iPhone?

How can I keep a custom UIButton highlighted on selection, for about 5 seconds and then redirect to other view? In my code it is not showing any color when selected rather redirecting to other view.
This is my code :
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CGRect rect=CGRectMake(0+70*j,yy,79,40);
UIButton *button=[[UIButton alloc] initWithFrame:rect];
[button setFrame:rect];
[button setContentMode:UIViewContentModeLeft];
button.titleLabel.font = [UIFont systemFontOfSize:14];
NSString *settitle=[NSString stringWithFormat:#"%#",item.TimeStart];
[button setTitle:settitle forState:UIControlStateNormal];
NSString *tagValue=[NSString stringWithFormat:#"%d%d",indexPath.section+1,i];
button.tag=[tagValue intValue];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[button setShowsTouchWhenHighlighted:YES];
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside ];
[hlcell.contentView addSubview:button];
[button release];
}
-(IBAction)buttonPressed:(id)sender
{
[sender setBackgroundColor:[UIColor cyanColor]];
Login *lgn=[[Login alloc] init];
[self presentModalViewController:lgn animated:NO];
[lgn release];
}
How can I do it ?
You have to set setShowsTouchWhenHighlighted:yes to the UIButton.
for example:
UIButton *click_btn=[UIButton buttonWithType:UIButtonTypeCustom];
click_btn.frame=CGRectMake(238,10, 70, 70);
[click_btn setTag:indexPath.row];
[click_btn setShowsTouchWhenHighlighted:YES];
[click_btn addTarget:self action:#selector(sample) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:click_btn];
you just add inside that method sleep(5);
for example:
This is user sample uibutton action method.
-(void)sample{
sleep(5);
sampleclass *classobject=[[sampleclass alloc]init];
[self.navigationController pushViewController:classobject animated:YES];
}
I wouldn't suggest using sleep() as this sleeps all threads and makes the app essentially dead for x seconds.
Highlight the button as per the instructions above and then use
[self performSelector:#selector(sample) withObject:self afterDelay:5.0f];
-(void)sample
{
UIViewController *myNewController = [[UIViewController alloc]init];
enter code here
[self presentModalViewController:myNewController animated:YES];
}

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.

How to display Array of values in tableview view for header in section?

I have one array for displaying values in table view section header. So, for displaying this array of values in section i used – tableView:viewForHeaderInSection:. Initially its displayed all values in array correctly but when I scrolling tableview this section values are changed instead of original values. please guide me what happening there.
i wrote like this in tableview Viewfor header in section
UIView *headerView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 323, 50)];
//Background Image
///UIImageView *headerBg=[[UIImageView alloc]initWithImage:[UIImage imageNamed:#"arrow-button_50.png"]];
/// [headerView addSubview:headerBg];
//Button
UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];
button.frame=CGRectMake(0, 0, 215, 70);
button.tag=section+1;
[button addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[button setImage:[UIImage imageNamed:#"down_arrow.png"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:#"right_arrow.png"] forState:UIControlStateSelected];
if([[self.cellcountarray objectAtIndex:section] intValue]==0)
button.selected=YES;
else button.selected=NO;
[headerView addSubview:button];
NearbyPeople *obj=[self.sectionArray objectAtIndex:section];
NSLog(#"first name from section %#",[self.sectionArray objectAtIndex:0]);
UILabel *headerTitle=[[UILabel alloc]initWithFrame:CGRectMake(30, 15, 300, 30)];
[headerTitle setBackgroundColor:[UIColor clearColor]];
[headerTitle setFont:[UIFont fontWithName:#"Helvetica-Bold" size:15]];
[headerTitle setTextColor:[UIColor whiteColor]];
headerTitle.text=[NSString stringWithFormat:#" %#",obj.FirstName];
NSLog(#"header text is %#",headerTitle.text);
///[headerTitle setText:obj.FirstName];
[headerView addSubview:headerTitle];
[headerTitle release];
Thanking in advance
It is not the problem of your data.So after end of your scrolling just [self.tableview reloadData]
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
[self.tableView reloadData];
}
May be it will work now.

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

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.