Nested Views-button is not clicking - iphone

i have a view which has a datepicker and a button added to it.
There is another view which adds the above view as subview.
But the events like touchesBegan and button's action are not being clicked on the subview.
Please help
The code of the parent view is:
iTagDatePicker *dt=[[iTagDatePicker alloc] initWithFrame:CGRectMake(0.0, 180.0, 320.0, 240.0)];
//dt.userInteractionEnabled=YES;
//[dt becomeFirstResponder];
dt.backgroundColor=[UIColor clearColor];
[UIView beginAnimations:#"animation" context:nil];
[UIView setAnimationDuration:1.0];
CGPoint cntr;
cntr.x=160.0;
cntr.y=420.0;
dt.center=cntr;
[self.view addSubview:dt];
self.view.userInteractionEnabled=YES;
CGPoint cntr1;
cntr1.x=160.0;
cntr1.y=158.0;
dt.center=cntr1;
[UIView commitAnimations];
[dt release];
and the code for the sub class is:
- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
dtPicker=[[UIDatePicker alloc] initWithFrame:CGRectMake(frame.origin.x, frame.origin.y, 320.0, 216.0)];
dtPicker.datePickerMode=UIDatePickerModeDate;
dtPicker.date=[NSDate date];
[self addSubview:dtPicker];
btn=[[UIButton buttonWithType:UIButtonTypeDetailDisclosure]retain];
btn.frame=CGRectMake(110.0, 400.0, 100.0, 20.0);
[btn setTitle:#"Done" forState:UIControlStateNormal];
btn.userInteractionEnabled=YES;
[btn becomeFirstResponder];
[btn addTarget:self action:#selector(SelectedVal:) forControlEvents:UIControlEventTouchDown];
[self addSubview:btn];
}
return self;
}
The button is not working

Subclass the parent view and pass touches to its children views.

Related

Add Button on my View

I'm creating a hidden form containing 3 buttons at the bottom of the parent view, by pressing another button, the view becomes visible, but those 3 buttons are not pressed, I don `t know what the problem is.
I add a button via code. Here is a method that is called when the button is pressed.
- (IBAction) addData:(id)sender
{
if(attachInfo==nil){
float screenHeight = self.view.frame.size.height;
attachInfo=[[UIView alloc] initWithFrame:CGRectMake(0, screenHeight, 320, 216)];
UIButton *attachImage = [UIButton buttonWithType:UIButtonTypeCustom];
//attachImage.enabled = YES;
[attachImage addTarget:self action:#selector(addImage:) forControlEvents:UIControlEventTouchUpInside];
[attachImage setFrame:CGRectMake(20, 20, 0, 0)];
[attachImage setImage:[UIImage imageNamed:#"add_photos"] forState:UIControlStateNormal];
[attachImage sizeToFit];
[attachInfo addSubview:attachImage];
UIButton *attachDoc = [UIButton buttonWithType:UIButtonTypeCustom];
//[pushButton addTarget:self action:#selector(pushViewController) forControlEvents:UIControlEventTouchUpInside];
[attachDoc setFrame:CGRectMake(120, 20, 80, 80)];
[attachDoc setImage:[UIImage imageNamed:#"add_docs.png"] forState:UIControlStateNormal];
[attachDoc sizeToFit];
[attachInfo addSubview:attachDoc];
UIButton *attachPlace = [UIButton buttonWithType:UIButtonTypeCustom];
//[pushButton addTarget:self action:#selector(pushViewController) forControlEvents:UIControlEventTouchUpInside];
[attachPlace setFrame:CGRectMake(220, 20, 80, 80)];
[attachPlace setImage:[UIImage imageNamed:#"add_place.png"] forState:UIControlStateNormal];
[attachPlace sizeToFit];
[attachInfo addSubview:attachPlace];
//[self.view addSubview:attachInfo];
[self.view addSubview:attachInfo];
[UIView animateWithDuration:0.3f animations:^{
CGRect frame = attachInfo.superview.frame;
frame.origin.y -= 216;
attachInfo.superview.frame=frame;
}];
}
else
{
[UIView animateWithDuration:0.3f animations:^{
CGRect frame = attachInfo.superview.frame;
frame.origin.y += 216;
attachInfo.superview.frame=frame;
}];
attachInfo=nil;
}
}
you need to add Action of buttons.
- (IBAction)first:(id)sender;
{
firstViewController * fvc=[FirstViewController alloc]init];
[FirstButton setHidden:False];
[self.navigationController pushViewController:fvc animated:YES];
}
- (IBAction)Second:(id)sender;
{
SecondViewController * svc=[SecondViewController alloc]init];
[SecondButton setHidden:False];
[self.navigationController pushViewController:svc animated:YES];
}
- (IBAction)Third:(id)sender;
{
ThirdViewController * tvc=[ThirdViewController alloc]init];
[ThirdButton setHidden:False];
[self.navigationController pushViewController:fvc animated:YES];
}
In the first button (attachImage), the height and the width is zero. So it should not be visible. For the other two buttons you haven't set the target. So those are not clickable.
for the first button set
[attachImage setFrame:CGRectMake(20, 20, 80, 80)];
and for the other two buttons add
//for the second button
[attachDoc addTarget:self action:#selector(addImage:) forControlEvents:UIControlEventTouchUpInside];
//for the third button
[attachPlace addTarget:self action:#selector(addImage:) forControlEvents:UIControlEventTouchUpInside];
-(void)createHiddenForm
{
hiddenView = [[UIView alloc] initWithFrame:CGRectMake(0,100,320,300)];
[hidden setHidden:TRUE];
[self.view addSubview:hiddenView];
float posY = 50.0;
for (int i=0 ; i<3 ; i++)
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setFrame:CGRectMake(50,posY, 100, 50)];
[button addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[hiddenView addSubview:button];
posY=posY+60;
}
}
-(void)showHiddenView
{
hidden.hidden = FALSE;
[self.view bringSubviewToFront:hiddenView];
}
-(void)buttonClicked:(id)sender
{
UIButton *senderBtn = (UIButton *)sender;
NSLog(#"Sender Button's tag = %#",senderBtn.tag);
}
Hope it will help you.
float screenHeight = self.view.frame.size.height;
attachInfo=[[UIView alloc] initWithFrame:CGRectMake(0, screenHeight, 320, 216)];
From the above, u r adding the attachInfo Y position outside the view boundaries.
Button onclick won't work when added outside the view.
To make it work replace below line.
[UIView animateWithDuration:0.3f animations:^{
CGRect frame = attachInfo.superview.frame;
frame.origin.y -= 216;
attachInfo.superview.frame=frame;
Instead of moving superview frame move attachInfo frame like this.
[UIView animateWithDuration:0.3f animations:^{
CGRect frame = attachInfo.frame;
frame.origin.y -= 216;
attachInfo.frame=frame;
So that attachInfo view will be added to the super view boundaries with animation.

My subview is not responding click event

I am creating a app that display subview like ActionSheetView and in that i have added UIGridView and UIPageControll thru outlet , but now when i click the cell it does not respond ,my subview is a ViewController with the nib file.
and if i create a button and add in my popupview programmatically then it wil displaying, and also get the click event, but anything in outlet does not respond.
i have tried to set userinteraction enable in my subview but it does not done any good.
here is my code for first view that display subview by clicking barbutton:
- (void)popUpView:(id)sender {
//self.view.backgroundColor = [UIColor darkGrayColor];
bGView = [[UIView alloc] init]; /this is the background view and all view is added in it
bGView.frame = CGRectMake(0,0,320,490);
bGView.backgroundColor = [UIColor clearColor];
bGView.alpha = 0.8 ;
CGRect viewFrame = CGRectMake(10, 90, 300, 300);
aPopUpViewController = [[PopUpViewController alloc] initWithNibName:#"PopUpViewController" bundle:nil]; //this is my view controller and when this shows on screen it display my greed view
aPopUpViewController.view.frame = viewFrame;
aPopUpViewController.view.userInteractionEnabled = YES;
[bGView addSubview:aPopUpViewController.view];
button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self
action:#selector(aMethod:)
forControlEvents:UIControlEventTouchDown];
button.backgroundColor = [UIColor clearColor];
[button setBackgroundImage:[UIImage imageNamed:#"close_button.png"] forState:UIControlStateNormal];
button.frame = CGRectMake(-1, 80, 30, 30);
[bGView addSubview:button];
//for view animation
/*
// from bottom to center animation
[bGView setFrame:CGRectMake( 0.0f, 480.0f, 320.0f, 480.0f)]; //notice this is OFF screen!
[UIView beginAnimations:#"animateTableView" context:nil];
[UIView setAnimationDuration:0.4];
[bGView setFrame:CGRectMake( 0.0f, 0.0f, 320.0f, 480.0f)]; //notice this is ON screen!
[UIView commitAnimations];*/
//for page like effect
/*[UIView transitionWithView:self.view duration:0.5
options:UIViewAnimationOptionTransitionCurlUp //change to whatever animation you like
animations:^ { [self.view addSubview:bGView]; }
completion:nil];*/
/*CATransition *transition = [CATransition animation];
transition.duration = 1.0;
transition.type = kCATransitionReveal; //choose your animation
[bGView.layer addAnimation:transition forKey:nil];
[self.view addSubview:bGView];*/
aPopUpViewController.view.userInteractionEnabled = YES;
bGView.userInteractionEnabled = YES;
self.view.userInteractionEnabled = YES;
bGView.transform = CGAffineTransformMakeScale(0.01, 0.01);
[UIView animateWithDuration:0.2 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
bGView.transform = CGAffineTransformIdentity;
} completion:^(BOOL finished){
// do something once the animation finishes, put it here
}];
[self.view addSubview:bGView];
addLabel.enabled = NO;
clipArt.enabled = NO;
emailItem.enabled = NO;
closeButton.enabled = NO;
self.navigationController.topViewController.title = #"Choose ClipArt";
ExampleAppDataObject* theDataObject = [self theAppDataObject];
theDataObject.navBarHide = #"no";
}
- (void)aMethod:(id)sender
{
//[aPopUpViewController.view removeFromSuperview];
//[button removeFromSuperview];
/*[UIView transitionWithView:self.view duration:0.5
options:UIViewAnimationOptionTransitionCrossDissolve//change to whatever animation you like
animations:^ { [bGView removeFromSuperview]; }
completion:nil];*/
[bGView removeFromSuperview];
addLabel.enabled = YES;
clipArt.enabled = YES;
emailItem.enabled = YES;
closeButton.enabled = YES;
self.navigationController.topViewController.title = #"Greeting's";
ExampleAppDataObject* theDataObject = [self theAppDataObject];
theDataObject.navBarHide = #"yes";
}
and this is how my screen look like
UPDATE
now i figured out my problem,problem is that i have given my whole view a tap listner,now i am adding grid view as subview so when ever i tap on cell rather then do select cell it will generate view tap gesture and thats why cell never selected,that's what i think..
add this line after you add every subviews in your main view..
[self.view bringSubviewToFront:yourGridView];

Disable keyboard on UITextField on edit

Since when I click the UITextField the date picker is showing along with the keyboard, I want to hide the key board operation on dob-text field?Here my code
- (void)removeViews:(id)object
{
[[self.view viewWithTag:9] removeFromSuperview];
[[self.view viewWithTag:10] removeFromSuperview];
[[self.view viewWithTag:11] removeFromSuperview];
}
- (void)dismissDatePicker:(id)sender
{
CGRect toolbarTargetFrame = CGRectMake(0, self.view.bounds.size.height, 320, 44);
CGRect datePickerTargetFrame = CGRectMake(0, self.view.bounds.size.height+44, 320, 216);
[UIView beginAnimations:#"MoveOut" context:nil];
[self.view viewWithTag:9].alpha = 0;
[self.view viewWithTag:10].frame = datePickerTargetFrame;
[self.view viewWithTag:11].frame = toolbarTargetFrame;
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(removeViews:)];
[UIView commitAnimations];
}
- (IBAction)but
{
//[eventText resignFirstResponder];
[dob resignFirstResponder];
if ([self.view viewWithTag:9])
{
return;
}
CGRect toolbarTargetFrame = CGRectMake(0, self.view.bounds.size.height-216-44, 320, 44);
CGRect datePickerTargetFrame = CGRectMake(0, self.view.bounds.size.height-216, 320, 216);
UIView *darkView = [[UIView alloc] initWithFrame:self.view.bounds ];
darkView.alpha = 0;
darkView.backgroundColor = [UIColor blackColor];
darkView.tag = 9;
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(dismissDatePicker:)] ;
[darkView addGestureRecognizer:tapGesture];
[self.view addSubview:darkView];
datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height+44, 320, 216)] ;
datePicker.datePickerMode=UIDatePickerModeDate;
datePicker.tag = 10;
[datePicker addTarget:self action:#selector(changeDate:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:datePicker];
UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height, 320, 44)] ;
toolBar.tag = 11;
toolBar.barStyle = UIBarStyleBlackTranslucent;
UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil] ;
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(dismissDatePicker:)] ;
[toolBar setItems:[NSArray arrayWithObjects:spacer, doneButton, nil]];
[self.view addSubview:toolBar];
[UIView beginAnimations:#"MoveIn" context:nil];
toolBar.frame = toolbarTargetFrame;
datePicker.frame = datePickerTargetFrame;
darkView.alpha = 0.5;
[UIView commitAnimations];
//[datePicker addTarget:self action:#selector(dateText:)forControlEvents:UIControlEventValueChanged];
//NSDateFormatter *_dateFormatter = [[NSDateFormatter alloc] init];
//_dateFormatter.dateStyle = NSDateFormatterFullStyle;
//dateText.text = [NSString stringWithFormat:#"%#",
// [_dateFormatter stringFromDate:datePicker.date]];
//[self.tableview reloadData];
}
I added the resignfirstresponder also,since its showing the same error
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
return [textField resignFirstResponder];
return [txt1 resignFirstResponder];//dob textfield
}
Use UITextField's delegate method
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if(textField == dob-text )
//load picker here
return NO; //hide keyboard
else
return YES; //show keyboard
}
A better solution is to set the UIPickerView as the inputView for the UITextField.
And you can set the UIToolBar as the inputAccessoryView of the inputView.
This way iOS will handle the displaying of all UIPickView.
Just set the properties top the correct views:
self.dateTextField.inputView = self.datePicker;
self.dateTextField.inputAccessoryView = self.inputToolbar;
On another note, getting views with viewWithTag: methods means that the you are looping thru all the views. Why not create properties for these views. It will be less messy and might be faster.
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
datePicker.hidden = NO;
[textField resignFirstResponder];
}
try in textFieldDidBeginEditing
-(void) textFieldDidBeginEditing:(UITextField *)textField{
[textField resignFirstResponder];
}
First of all a textfield should not have a popover - neither picker nor popover nor anything
Best solution would be to put a button - type custom - set image like a date selector image and add selector - in that case you would n't have any issues w.r.t textfields popover and the look would be good as well and you can customize it as per your requirement.

how to remove a view?

I have an iphone application in which i am adding a view controllers view with an animation like flipfrom left.I was doing it with navigationbarbutton item.when clicking on it i was adding a view controllers view to my rootview.and changing the button amd in that action i am removing that view and adding the previous button like that.but the pbm i need to remove that info view with in a button action inside that view.here is my code for the flipanimation.`
- (void)backPressed1
{
InfoViewController *infoviewcontroller = [[InfoViewController alloc] initWithNibName:#"InfoViewController" bundle:nil];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:.95];
//[UIView beginAnimations:nil context:NULL];
//[UIView setAnimationDuration:0.5];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view cache:YES];
//CGRect frame=CGRectMake(0,0,320,480);
[rootview removeFromSuperview];
//[infoviewcontroller.view setFrame:frame];
//selfhelpscoresAppDelegate *appdelegate=[[UIApplication sharedApplication] delegate];
[infoviewcontroller viewWillAppear:YES];
[self.view addSubview:infoviewcontroller.view];
[UIView commitAnimations];
UIImage *buttonImage = [UIImage imageNamed:#"information_btn.png"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:buttonImage forState:UIControlStateNormal];
button.frame = CGRectMake(0, 0, 42, 32);
[button addTarget:self action:#selector(backpressed) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.rightBarButtonItem = customBarItem;
//UILabel *label =[[[UILabel alloc]init]autorelease];
self.navigationItem.title=#"Tutorial";
//label.text = #"";
//self.navigationController.title = #"";
[customBarItem release];
}
-(IBAction)backpressed
{
InfoViewController *infoviewcontroller = [[InfoViewController alloc] initWithNibName:#"InfoViewController" bundle:nil];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:.95];
//[UIView beginAnimations:nil context:NULL];
//DailypercentageViewController *pieChart = [[DailypercentageViewController alloc] initWithNibName:#"DailypercentageViewController" bundle:nil];
//[UIView setAnimationDuration:0.5];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.view cache:YES];
//CGRect frame=CGRectMake(0,0,320,370);
[infoviewcontroller.view removeFromSuperview];
[self.view addSubview:rootview];
[UIView commitAnimations];
//[rearView setFrame:frame];
//[pieChart viewDidAppear:YES];
UIImage *buttonImage1 = [UIImage imageNamed:#"information_btn.png"];
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
[button1 setImage:buttonImage1 forState:UIControlStateNormal];
button1.frame = CGRectMake(0, 0, 42, 32);
[button1 addTarget:self action:#selector(backPressed1) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *customBarItem1 = [[UIBarButtonItem alloc] initWithCustomView:button1];
self.navigationItem.rightBarButtonItem = customBarItem1;
[customBarItem1 release];
//UILabel *label1 =[[[UILabel alloc]init]autorelease];
self.navigationItem.title=#"";
//label1.text = #"Home";
//self.title= #"Home";
}
`my problem is how i can remove this infoview and came back to the rootview from with in the button action from the infoview?

How to flip a UIButton horizontal with another UIButton on the backside?

I want a button that has a another button on the backside. When you press the button it should flip horizontal and show the other button. Apple did this in the ipod-App. When you hear a song you can press the button on the right site of the navigationbar and then the view flips and you can see all the songs from the album. But the interest point is that the button on the right side flips with the view horizontal.
How can I do this?
alt text http://img51.imageshack.us/img51/585/welyrics2jpg.png
Thanks for the answer!
I dont understand this atm.
My iPhone coach did it for me a while back.. but i wasnt looking at what he did.
So heres the code.
.h
#import <UIKit/UIKit.h>
#interface flipTesViewController : UIViewController {
IBOutlet UIView *containerView;
UIImageView *heads;
UIImageView *tails;
}
-(IBAction)test;
#end
.m
#import "flipTesViewController.h"
#implementation flipTesViewController
-(IBAction)test{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:containerView cache:YES];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(test)];
[UIView setAnimationDuration:.5];
if(heads.tag==1){
[containerView addSubview:heads];
heads.tag=2;
}
else{
[containerView addSubview:tails];
heads.tag=1;
}
[UIView commitAnimations];
}
- (void)viewDidLoad {
[super viewDidLoad];
heads = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"NZHeads.png"]];
tails = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"NZTails.png"]];
[containerView addSubview:heads];
}
//other method built in to view based template
#end
On 3.x there's a simpler way:
Make the "backside view" as a UIViewController or one of its subclasses.
When the your "flip" button is pressed, call
.
backsideViewCtrler.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:backsideViewCtrler animated:YES];
-(void)viewDidLoad
{
UIButton *btn; = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(x, y, 60, 40);
btn.backgroundColor = [UIColor clearColor];
[btn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
btn.tag=count;
[buttonWithTag setBackgroundImage:[UIImage imageNamed:#"2.png"] forState:UIControlStateNormal];
[btn setTitle:[NSString stringWithFormat:#"%d",btn.tag] forState:UIControlStateNormal];
[view_frame addSubview:btn];
[btn addTarget:self action:#selector(Click:)forControlEvents:UIControlEventTouchUpInside];
}
//action
- (IBAction)Click:(id)sender
{
UIButton *click_btn = (UIButton *)sender;
NSLog(#"%d",click_btn.tag);
UIButton *buttonWithTag = (UIButton *)[view_frame viewWithTag:click_btn.tag];
CATransition *animation = [CATransition animation];
animation.delegate = self;
animation.duration = 1.0;
animation.timingFunction = UIViewAnimationCurveEaseInOut;
animation.fillMode = kCAFillModeForwards;
animation.removedOnCompletion = NO;
animation.type = #"oglFlip";
[buttonWithTag.layer addAnimation:animation forKey:nil];
[buttonWithTag setBackgroundImage:[UIImage imageNamed:#"1.png"] forState:UIControlStateNormal];
}