Renaming a button after user input - objective-c

I am a noob with Xcode and programming in general. I have a code so that a user can click on a button "Start Date" which then brings up a pop-up date picker. I want to know how to have the button "Start Date" rename itself to the date the user selects and then also allow it to be re-edited.
Here is the code I have so far:
- (void)changeDate:(UIDatePicker *)sender {
NSLog(#"New Date: %#", sender.date);
}
- (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)StartDate:(UIButton *)sender {
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];
UIDatePicker *datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height+44, 320, 216)];
datePicker.tag = 10;
datePicker.datePickerMode = UIDatePickerModeDate;
[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];
}

Related

Programatically created UIDatepicke done and cancle button is not working

This is my UIDatePicker code but in that picker, done and cancle button is not working ...please help me.... i also add here my done and cancle button methods here..Please give me a proper suggessions for how it works properly.
if (textField.tag==Jobdatetxt.tag)
{
[Jobdatetxt resignFirstResponder];
[data.toolbar removeFromSuperview];
UITextField *lagFreeField = [[UITextField alloc] init];
[self.view.window addSubview:lagFreeField];
[lagFreeField becomeFirstResponder];
[lagFreeField resignFirstResponder];
[lagFreeField removeFromSuperview];
//////////////////////////////////////////////// DATE OF JOB////////////////////////////////////////////////
pickerDate = [[UIDatePicker alloc] initWithFrame:CGRectMake(0,result.height - 216, result.width, 216)];
[pickerDate setDatePickerMode:UIDatePickerModeDate];
pickerDate.backgroundColor = [UIColor whiteColor];
[self.view.window addSubview:pickerDate];
toolbar = [[UIToolbar alloc] init];
toolbar.barStyle = UIBarStyleBlackOpaque;
toolbar.frame=CGRectMake(0,result.height - 266,result.width,50);
cancel = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[cancel setTitle:#"Cancel" forState:UIControlStateNormal];
cancel.backgroundColor = [UIColor clearColor];
[cancel.titleLabel setFont:[UIFont boldSystemFontOfSize:18]];
cancel.titleLabel.textColor = [UIColor whiteColor];
[cancel addTarget:self action:#selector(CancelClicked:) forControlEvents:UIControlEventTouchUpInside];
cancel.tag = textField.tag;
cancel.frame = CGRectMake(10, 10, 70, 30);
[toolbar addSubview:cancel];
doneButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[doneButton setTitle:#"Done" forState:UIControlStateNormal];
doneButton.backgroundColor = [UIColor clearColor];
[doneButton.titleLabel setFont:[UIFont boldSystemFontOfSize:18]];
doneButton.titleLabel.textColor = [UIColor whiteColor];
[doneButton addTarget:self action:#selector(DoneClicked:) forControlEvents:UIControlEventTouchUpInside];
doneButton.tag = textField.tag;
doneButton.frame = CGRectMake(result.width - 80, 10, 70, 30);
[toolbar addSubview:doneButton];
[self.view.window addSubview:toolbar];
}
// Done Button method
-(IBAction)DoneClicked:(UIButton *)sender
{
[pickerDate removeFromSuperview];
[toolbar removeFromSuperview];
}
//Cancle Button Method
-(IBAction)CancelClicked:(UIButton *)sender
{
[pickerDate removeFromSuperview];
[toolbar removeFromSuperview];
}

How actions to Next/Previous Keyboard ToolbarCustom iOS7

I am trying to set the next/previous buttons on my keyboard toolbar, I want get put in navigation to go next/Previous textfield. Here is what I am trying.
How can I use the system bar button item to change to textfield?
-(void)addTeam{
UIView * view = [[UIView alloc]initWithFrame:CGRectMake(15, (0+(50*[teamsArray count])), 273, 50)];
[view setTag:[teamsArray count]+1];
UILabel *lblTeamName = [[UILabel alloc ] initWithFrame:CGRectMake(5,0,58,20)];
[lblTeamName setBackgroundColor:[UIColor clearColor]];
lblTeamName.font = [UIFont fontWithName:#"Arial Rounded MT Bold" size:(12.0)];
lblTeamName.text = [NSString stringWithFormat:#"Team %i",[teamsArray count]+1];
[lblTeamName setTag:7];
[view addSubview:lblTeamName];
UITextField* txtTeamName = [[UITextField alloc]initWithFrame:CGRectMake(5,20,150,30)];
[txtTeamName setBorderStyle:UITextBorderStyleRoundedRect];
[txtTeamName setPlaceholder:#"Team Name"];
[txtTeamName addTarget:self action:#selector(hideKeyboard) forControlEvents:UIControlEventEditingDidEndOnExit];
[txtTeamName setDelegate:self];
[view addSubview:txtTeamName];
UIButton *btnDelete = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btnDelete addTarget:self
action:#selector(btnDelete_OnTouch:)
forControlEvents:UIControlEventTouchDown];
[btnDelete setTag:[teamsArray count]];
[btnDelete setTitle:#"-" forState:UIControlStateNormal];
btnDelete.frame = CGRectMake(160, 20, 20, 30);
[view addSubview:btnDelete];
[scrollView addSubview:view];
[teamsArray addObject:view];
}
-(BOOL)textFieldShouldBeginEditing: (UITextField *)textField
{
UIToolbar * keyboardToolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];
keyboardToolBar.barStyle = UIBarStyleDefault;
[keyboardToolBar setItems: [NSArray arrayWithObjects:
[[UIBarButtonItem alloc]initWithTitle:#"Previous" style:UIBarButtonItemStyleBordered target:self action:#selector(previousTextField)],
[[UIBarButtonItem alloc] initWithTitle:#"Next" style:UIBarButtonItemStyleBordered target:self action:#selector(nextTextField)],
[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
[[UIBarButtonItem alloc]initWithTitle:#"Done" style:UIBarButtonItemStyleDone target:self action:#selector(resignKeyboard)],
nil]];
textField.inputAccessoryView = keyboardToolBar;
}
- (void)nextTextField {
????
}
-(void)previousTextField
{
????
}
you could use next and previous button,
How to navigate through textfields (Next / Done Buttons)
And do this in ViewDidLoad
UIToolbar * keyboardToolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];
keyboardToolBar.barStyle = UIBarStyleDefault;
[keyboardToolBar setItems: [NSArray arrayWithObjects:
[[UIBarButtonItem alloc]initWithTitle:#"Previous" style:UIBarButtonItemStyleBordered target:self action:#selector(previousTextField)],
[[UIBarButtonItem alloc] initWithTitle:#"Next" style:UIBarButtonItemStyleBordered target:self action:#selector(nextTextField)],
[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
[[UIBarButtonItem alloc]initWithTitle:#"Done" style:UIBarButtonItemStyleDone target:self action:#selector(resignKeyboard)],
nil]];
textField.inputAccessoryView = keyboardToolBar;

iOS – Animation glitch with UIToolbar

I'm getting a "weird" animation glitch when I add a UIToolbar to the view. I'm animating the UIToolbar so it slides up from the bottom together with either a UIPickerView or a UIDatePicker. At first I see it's sliding up but after the animation is finished it quickly disappears. This happens when I slide down (animating the UIPickerView to slide down off the screen) and right after that I have another UIPickerView slide up.
I noticed that if I set a delay (- performSelector...) on the slide up call for 0.3 seconds it will display the UIToolbar properly (anything less than 0.3 seconds will still have the same glitch). What might be causing this?
EDIT: Perhaps I should place both the UIToolbar and UIDatePicker in a new UIView container?
This is the code I'm using:
if ([self.view.subviews containsObject:self.dateRepeatPicker]) {
[self dismissDateRepeatPickerSegmentChanged:NO];
// "Hack", if I don't delay this call the UIToolbar will not display
[self performSelector:#selector(showDatePicker) withObject:nil afterDelay:0.3];
} else if (![self.view.subviews containsObject:self.datePicker]) {
[self showDatePicker];
}
- (void)dismissDateRepeatPickerSegmentChanged:(BOOL)segmentChanged {
CGRect toolbarTargetFrame = CGRectMake(0, self.view.bounds.size.height, 320, 44);
CGRect dateRepeatPickerTargetFrame = CGRectMake(0, self.view.bounds.size.height+44, 320, 216);
[UIView animateWithDuration:0.2
animations:^{
self.pickerToolbar.frame = toolbarTargetFrame;
self.dateRepeatPicker.frame = dateRepeatPickerTargetFrame;
}
completion:^(BOOL finished) {
if (segmentChanged) {
[self.pickerToolbar removeFromSuperview];
[self.dateRepeatPicker removeFromSuperview];
[self.remindMeTableView reloadData];
} else {
[self.pickerToolbar removeFromSuperview];
[self.dateRepeatPicker removeFromSuperview];
}
}];
}
- (void)showDatePicker {
// Create the Toolbar over the Picker
CGRect toolbarTargetFrame = CGRectMake(0, self.view.bounds.size.height-216-44, 320, 44);
if (self.pickerToolbar == nil) {
self.pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height, 320, 44)];
self.pickerToolbar.barStyle = UIBarStyleBlack;
self.pickerToolbar.translucent = YES;
}
UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"picker-dismiss.png"] style:UIBarButtonItemStyleBordered target:self action:#selector(dismissDatePickerToolbar)];
[self.pickerToolbar setItems:[NSArray arrayWithObjects:spacer, doneButton, nil]];
[self.view addSubview:self.pickerToolbar];
// Create the Picker under the Toolbar
CGRect datePickerTargetFrame = CGRectMake(0, self.view.bounds.size.height-216, 320, 216);
if (self.datePicker == nil) {
self.datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height+44, 320, 216)];
[self.datePicker setMinuteInterval:5];
[self.datePicker addTarget:self action:#selector(changeDate:) forControlEvents:UIControlEventValueChanged];
}
[self.view addSubview:self.datePicker];
[UIView animateWithDuration:0.2 animations:^{
self.pickerToolbar.frame = toolbarTargetFrame;
self.datePicker.frame = datePickerTargetFrame;
}];
}
Solved by adding the UIPickerView and the UIToolbar to a containing UIView and then animating the containing UIView.

UIBarbuttonItem disappears

I have the following function which displays the top bar on my app. It consists of an image and a UIBarButton. I call it from viewWillAppear. When I push the current controller and move to the next controller and then move Back to the main controller, the right bar button disappears. Please help.
-(void)setTopBar
{
self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:0.6367 green:0.6367 blue:0.6367 alpha:1.0];
UIImage *titleImage = [UIImage imageNamed: MAIN_TOPBAR];
if([self.navigationController.navigationBar respondsToSelector:#selector(setBackgroundImage:forBarMetrics:)] ) {
//iOS 5 new UINavigationBar custom background
[self.navigationController.navigationBar setBackgroundImage:titleImage forBarMetrics: UIBarMetricsDefault];
// NSLog(#"loading nav items");
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:#"More Apps" style:UIBarButtonItemStylePlain target:self action:#selector(loadFAADAds)];
rightButton.tag = 11111;
self.navigationItem.rightBarButtonItem = rightButton;
[rightButton release];
}
else
{
CGRect titleFrame = self.navigationController.navigationBar.frame;
UIImageView *titleImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, titleFrame.size.width,titleFrame.size.height)];
[titleImageView setImage:titleImage];
[titleImageView setTag:TOPBAR_TAG];
// [self.navigationController.navigationBar sendSubviewToBack:titleImageView];
// self.navigationItem.titleView = titleImageView;
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:#"More Apps" style:UIBarButtonItemStylePlain target:self action:#selector(loadFAADAds)];
self.navigationItem.rightBarButtonItem = rightButton;
//self.navigationController.navigationItem.rightBarButtonItem = rightButton;
[self.navigationController.navigationBar insertSubview:titleImageView atIndex:0];
[self.navigationController.navigationBar sendSubviewToBack:self.navigationController.navigationBar];
[rightButton release];
}
[titleImage release];
}
-(void)setTopBar
{
self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:0.6367 green:0.6367 blue:0.6367 alpha:1.0];
UIImage *titleImage = [UIImage imageNamed: MAIN_TOPBAR];
if([self.navigationController.navigationBar respondsToSelector:#selector(setBackgroundImage:forBarMetrics:)] ) {
//iOS 5 new UINavigationBar custom background
[self.navigationController.navigationBar setBackgroundImage:titleImage forBarMetrics: UIBarMetricsDefault];
// NSLog(#"loading nav items");
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:#"More Apps" style:UIBarButtonItemStylePlain target:self action:#selector(loadFAADAds)];
rightButton.tag = 11111;
self.navigationItem.rightBarButtonItem = rightButton;
// [rightButton release];
}
else
{
CGRect titleFrame = self.navigationController.navigationBar.frame;
UIImageView *titleImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, titleFrame.size.width,titleFrame.size.height)];
[titleImageView setImage:titleImage];
[titleImageView setTag:TOPBAR_TAG];
// [self.navigationController.navigationBar sendSubviewToBack:titleImageView];
// self.navigationItem.titleView = titleImageView;
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:#"More Apps" style:UIBarButtonItemStylePlain target:self action:#selector(loadFAADAds)];
self.navigationItem.rightBarButtonItem = rightButton;
//self.navigationController.navigationItem.rightBarButtonItem = rightButton;
[self.navigationController.navigationBar insertSubview:titleImageView atIndex:0];
[self.navigationController.navigationBar sendSubviewToBack:self.navigationController.navigationBar];
//[rightButton release];
}
// [titleImage release];
}

How can I read the coordinates of a button?

UIView *contentView = [[UIView alloc] initWithFrame: [[UIScreen mainScreen] applicationFrame]];
contentView.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:#"background.jpg"]];
self.view = contentView;
[contentView release];
[contentView addSubview:scrollView];
scrollView.contentSize = CGSizeMake(1800,480);
UIScrollView* tscrollView = [[UIScrollView alloc] initWithFrame:(CGRect){.origin.x = 0.0f, .origin.y = 0.0f, .size.width = 320.0f, .size.height = 5480.0f}];
tscrollView.contentSize = CGSizeMake(1800,192);
Object1 = [UIButton buttonWithType:1];
Object1.frame = CGRectMake(383, 250, 256, 192);
UIImage *buttonImage = [UIImage imageNamed:#"02_1024_768.jpg"];
[Object1 setBackgroundImage:buttonImage forState:UIControlStateNormal];
//[self.view addSubview:Object1];
[scrollView addSubview:Object1];
Object5 = [UIButton buttonWithType:1];
Object5.frame = CGRectMake(1487, 250, 256, 192);
UIImage *buttonImage1 = [UIImage imageNamed:#"p5.jpg"];
[Object5 setBackgroundImage:buttonImage1 forState:UIControlStateNormal];
[scrollView addSubview:Object5];
UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(400, 100, 200, 200)];
[myLabel setBackgroundColor:[UIColor clearColor]];
myLabel.text = #"Button1 is in range";
[myLabel setTextAlignment:UITextAlignmentCenter];
[myLabel setTextColor:[UIColor whiteColor]];
NSLog(#"x = %d",Object1.center.x);
position = CGPointMake(0,0);
Object1.center = CGPointMake(Object1.center.x+position.x,Object1.center.y+position.y);
if((Object1.center.x >341) && (Object1.center.x < 597)){
[myLabel setHidden:NO];
}
else {
[myLabel setHidden:YES];
}
[self.view addSubview:myLabel];
Here is the code, but seems that the x coordinates always equals to 0. Could some1 tell me why and how to fix it? Thanks in advance.
Try this
NSLog(#"x=%f",Object1.center.x);
coordinates are float value and you print like int so you get 0. i check your code in project