UIAlertView button is disabled even after picking date from UIDatePicker - ios7

The problem is that after picking date from UIDatePicker the "Ok" button of UIAlertView is disabled.I tried ,but no luck.
-(BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView
should be called whenever we type anything in textfield,but when date is entered it's not getting called,but if i type anything from keyboard,its ok then.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.section == 0 && indexPath.row == 1)
{
alertView1 = [[UIAlertView alloc] initWithTitle:nil message:nil delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Done",nil];
alertView1.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;
alertView1.tag=1;
alertText = [alertView1 textFieldAtIndex:0];
itemText = [alertView1 textFieldAtIndex:1];
alertText.inputView=datePicker;
itemText.inputView=secondPicker;
[datePicker addTarget:self action:#selector(firstTF) forControlEvents:UIControlEventValueChanged];
[secondPicker addTarget:self action:#selector(secondTF) forControlEvents:UIControlEventValueChanged];
[alertText setPlaceholder:#"From Date"];
[itemText setPlaceholder:#"To Date"];
itemText.secureTextEntry = NO;
[alertView1 show];
}
- (void)firstTF
{
NSDate *date = datePicker.date;
NSDateFormatter *dateFormat = [[NSDateFormatter alloc]init];
[dateFormat setDateStyle:NSDateFormatterMediumStyle];
alertText.text = [dateFormat stringFromDate:date];
}
- (void)secondTF
{
NSDate *date = secondPicker.date;
NSDateFormatter *dateFormat = [[NSDateFormatter alloc]init];
[dateFormat setDateStyle:NSDateFormatterMediumStyle];
itemText.text = [dateFormat stringFromDate:date];
}
- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView
{
NSString *inputText = [[alertView textFieldAtIndex:0] text];
if( [inputText length] > 0)
{
NSLog(#"alertViewShouldEnableFirstOtherButton: was called!");
return YES;
}
else
{
return NO;
}
}

This happens because of "alertViewShouldEnableFirstOtherButton" called before you enter the text in to text field. As per your condition in side the method "Done" button will be disable. Remove this method and you will achieve your target.

Related

Remove all controls (subviews) in UITabBarViewController

I'm fairly new to ios programming and I encountered an issue lately. I'm trying to navigate to a selected tab of a tabbarviewcontroller programmatically. Upon entering that selected tab, remove all controls in it and repopulate it with different controls.
I managed to navigate to that selected tab programmatically, however, i'm unable to remove all the subviews.
Below are snippet of my codes..
- (IBAction)btnSave:(id)sender {
orthoPaedicInjuryTabBarViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"OrthoInjuryTabBarVC"];
orthoPaedicInjuryTabBarViewController.selectedIndex = 2;
[self presentViewController:orthoPaedicInjuryTabBarViewController animated:YES completion:nil];
self.alert = [[UIAlertView alloc] initWithTitle:#"Empty Field, Step3"
message: #"Please fill up the frequency of exercises"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[self.alert show];
}
- (void)viewWillAppear:(BOOL)animated {
Store *MyStore = [Store SharedStore];
OrthoInjury *orthoInjury = MyStore.orthoInjury;
NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setDateFormat:#"dd-MM-yyyy"];
NSString *Dob = [format stringFromDate:orthoInjury.dateOfInjury];
self.lblDateOfInjury.text= Dob;
// Do any additional setup after loading the view, typically from a nib.
self.typeOfExercisesList = [Utility GetTypesOfExercisesList];
self.pickerViewTypesOfexercises.dataSource = self;
self.pickerViewTypesOfexercises.delegate = self;
TherapyPhases *therapyPhases = MyStore.therapyPhases;
self.tbxNumberOfWeeks.text = #(therapyPhases.durationWeeks).stringValue;
if([[orthoInjury.natureOfInjury uppercaseString] isEqualToString:[#"Repetitive Strain Injury" uppercaseString]])
{
NSArray *subArray = [self.view subviews];
if([subArray count] != 0) {
for(int i = 1 ; i < [subArray count] ; i++) {
[[subArray objectAtIndex:i] removeFromSuperview];
/*if ([[subArray objectAtIndex:i] isKindOfClass:[UILabel class]])
{
NSLog(#"%#",[[subArray objectAtIndex:i] text]);
}*/
}
}
}
}
Try below for removing the subviews:-
NSMutableArray *subVws=[self.tabBarController.view.subviews mutableCopy];
[subVws performSelector:#selector(removeFromSuperview)];

EKEvent not passing title to view

I'm having problems when creating a calendar event in my app, i have a collection view that have dates when you click one of them the view controller pops out without the name or date that i passed by code.
Here's the code:
EKEventStore *eventStore = [[EKEventStore alloc] init];
if([eventStore respondsToSelector:#selector(requestAccessToEntityType:completion:)]) {
//This code will run when uses has made his/her choice
EKEvent *event = [EKEvent eventWithEventStore:eventStore];
[event setTitle:theTitle];
event.startDate = date;
event.endDate = date;
EKEventEditViewController *controller = [[EKEventEditViewController alloc] init];
controller.navigationBar.backgroundColor = [UIColor blackColor];
[event setCalendar:[eventStore defaultCalendarForNewEvents]];
NSError *err;
[eventStore saveEvent:event span:EKSpanThisEvent error:&err];
NSLog(#"err %#", err);
controller.eventStore = eventStore;
controller.editViewDelegate = self;
[self presentViewController:controller animated:YES completion:nil];
}
The code is in the
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
Does EKEventStore have special methods to set the date or title?

Highlight UITableViewCell while pickerview event

I have a UI which contains a UITableView and a UIDatePicker. When I select a Row on my table it is highlighted, but as soon as I interact with the DatePicker (scroll to a new value) the row isn't highlighted anymore. The value of the row is bound to that of the DatePicker. How can I permanently highlight the cell?
In the iCal-App -> Add -> Start&End I saw this behaviour.
I tried the selectRowAtIndex method in my ValueChanged-method of the picker. I thought that the highlighting is lost in this callback-method.
-(IBAction)setStartTimeMethod
{
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"EEE, dd. MMM yyyy HH:mm"];
if(!self.endDateRowSelected)
self.date = [dateFormat stringFromDate:[datePicker date]];
else
self.endDate = [dateFormat stringFromDate:[datePicker date]];
[dateFormat release];
// [self.tableView1 selectRowAtIndexPath:tableView1.indexPathForSelectedRow animated:NO scrollPosition:UITableViewScrollPositionTop];
[self.tableView1 reloadData];
}
Edit, my didSelectRowMethod
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"EEE, dd. MMM yyyy HH:mm"];
if(indexPath.row == 0){
NSLog(#"Start");
self.endDateRowSelected = NO;
NSDate *startDate = [formatter dateFromString:self.date];
[datePicker setDate:startDate animated:YES];
//if(self.endDateRowSelected)
// [tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
}else if(indexPath.row == 2){
NSLog(#"End");
self.endDateRowSelected = YES;
if(self.endDate){
NSDate *endDateTime = [formatter dateFromString:self.endDate];
[datePicker setDate:endDateTime animated:YES];
}
}else{
self.endDateRowSelected = NO;
}
[formatter release];
// Won't work, doesn't allow highlighting a cell.
// [tableView deselectRowAtIndexPath:indexPath animated:NO];
}
BR,
mybecks
Use this piece of code in the end of didSelectRowAtIndexPath delegate method of table view,
[tableView deselectRowAtIndexPath:indexPath animated:NO];
Try:
[self.tableView1 reloadData];
// and then
[self.tableView1 selectRowAtIndexPath:tableView1.indexPathForSelectedRow animated:NO scrollPosition:UITableViewScrollPositionTop];
I guess a much better way of doing this is with IBOutlets for each custom cell in your table. If you are editing the date for that cell, then why reload the entire table? If you have less number of cells, you can go ahead and create custom UITableViewCell, add labels and update only those labels from your UIDatePicker delegate callbacks.
Use the following code in didSelectRowAtIndexPath
cell.selectionStyle=UITableViewCellSelectionStyleBlue;
The solution was quite easy.
In my cellForRowAtIndexPath I must call the selectRowAtIndexPath method.
...
if(indexPath.row == 0){
cell.description.text = [dict objectAtIndex:indexPath.row];
cell.dateTime.text = self.startDate;
if(!endDateRowSelected)
[tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
}else if(indexPath.row == 2){
cell.description.text = [dict objectAtIndex:indexPath.row];
cell.dateTime.text = self.endDate;
if(endDateRowSelected)
[tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
}
...
The bool is set in my didSelectRowAtIndexPath, depending on clicking on Start or End.
I still reload the table in my pickerValueChanged delegate.
Thank you for time & help.

Realtime Values in UITableViewCell from UIDatePicker

I have a UIViewController with two UI Components: UITableView and UIDatePicker. My goal is, to create a screen like the ical->add->Start & End.
I added the the tableView delegate and dateSource to my header file. In the implementation file, I want to display the current value from the datePicker in my tableCell (like the iCal Start & End screen).
My .m looks like the following:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
[datePicker addTarget:self action:#selector(datePickerValueChanged) forControlEvents:UIControlEventValueChanged];
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
NSDictionary *dictionary = [listOfItems objectAtIndex:indexPath.section];
NSMutableArray *dict = [dictionary objectForKey:#"DATA"];
if(indexPath.row == 1){
UISwitch *switchSpecifyEnd = [[[UISwitch alloc] initWithFrame:CGRectZero] autorelease];
cell.accessoryView = switchSpecifyEnd;
[(UISwitch *)cell.accessoryView setOn:NO]; // Or NO, obviously!
[(UISwitch *)cell.accessoryView addTarget:self action:#selector(switchSpecifyEnd)
forControlEvents:UIControlEventValueChanged];
cell.textLabel.text = [dict objectAtIndex:indexPath.row];
}else if(indexPath.row == 3){
UISwitch *switchOpenEnd = [[[UISwitch alloc] initWithFrame:CGRectZero] autorelease];
cell.accessoryView = switchOpenEnd;
[(UISwitch *)cell.accessoryView setOn:NO]; // Or NO, obviously!
[(UISwitch *)cell.accessoryView addTarget:self action:#selector(switchOpenEnd)
forControlEvents:UIControlEventValueChanged];
cell.textLabel.text = [dict objectAtIndex:indexPath.row];
}else{
cell.textLabel.text = [dict objectAtIndex:indexPath.row];
// NSLog(#"Update Cell: %#",<but how>);
}
return cell;
}
- (void)datePickerValueChanged{
NSLog(#"Logging: %#", [NSString stringWithFormat:#"%#",datePicker.date]);
}
How can I update my tableCell with the value from the datePicker?
BR,
mybecks
For updating your table cell you need to reload your table and when your table will reload then cellForRowAtIndexPath will get call and you can update your cell text.
Suppose this is your method that gets called when you select some date in date picker
-(IBAction)setStartTimeMethod
{
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"EEE MMM dd, yyyy hh:mm a"];
self.startTimeString = [dateFormat stringFromDate:[timeStartPicker date]];
[dateFormat release];
[optionsTableView reloadData];
}

Objective C: How to use addTarget:action:forControlEvents: method?

I am trying to update the date and time displayed in a table cell immediately after the UIPicker's data has changed (real time updated). I implemented the following code. My "update" method is not being called despite changing the values in the picker. Can anyone advise? Thanks!
Zhen
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSUInteger row = [indexPath row];
if (row == 0)
{
self.picker.hidden = NO;
[self.picker addTarget:self action:#selector(updateDate) forControlEvents:UIControlEventTouchUpInside];
}
}
- (void)updateDate
{
selectedDate = [self.picker date];
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:#"dd-MM-yyyy HH:mm"];
selectedDateString = [formatter stringFromDate:selectedDate];
[tableView reloadData];
}
You need to put a colon after the selector name.
[self.picker addTarget:self action:#selector(updateDate:) forControlEvents:UIControlEventTouchUpInside];
Also, the updateDate method should take an object of type id.
- (void) updateDate:(id) obj { }
change in your code -
[self.picker addTarget:self action:#selector(updateDate:) forControlEvents:UIControlEventTouchUpInside];
to
[self.picker addTarget:self action:#selector(updateDate:) forControlEvents:UIControlEventAllEvents];
you can also handle it in UIPicker delegates.