Hi all,
I have a couple of problems with the Xcode programming for IOS. I'm a newbie in Xcode programming, and english is not my first language. So please bear with me.
Basically, I have a tableview with 2 custom tableviewcells. I have tap gesture recognition in tableview. In one of the custom tableviewcells I have a uitextfield for selecting the date. I used to be using action sheet, but since I use the newest Xcode, it generate warning. So I switch to using uiview show the date picker with one toolbar button, done button.
The first problem that I encountered was the button click was not registered to target selector of the button. I searched the google, and found out that the tap gesture in tableview was the problem. So I create another tap gesture to point to the view. It works.
When I click the uitextfield in tableview, it shows the uiview (half screen) with date picker and done button. It works as expected, even the date picker delegate works correctly.
The problem was, in the selector target of the gesture recognition, I can't refer to uiview that I created before. So I'm at lost how to dismiss the uiview after the button was click.
Thanks all for your help
I have a couple of problems with the Xcode programming for IOS. I'm a newbie in Xcode programming, and english is not my first language. So please bear with me.
Basically, I have a tableview with 2 custom tableviewcells. I have tap gesture recognition in tableview. In one of the custom tableviewcells I have a uitextfield for selecting the date. I used to be using action sheet, but since I use the newest Xcode, it generate warning. So I switch to using uiview show the date picker with one toolbar button, done button.
The first problem that I encountered was the button click was not registered to target selector of the button. I searched the google, and found out that the tap gesture in tableview was the problem. So I create another tap gesture to point to the view. It works.
When I click the uitextfield in tableview, it shows the uiview (half screen) with date picker and done button. It works as expected, even the date picker delegate works correctly.
The problem was, in the selector target of the gesture recognition, I can't refer to uiview that I created before. So I'm at lost how to dismiss the uiview after the button was click.
Code:
FEInsertUpdateTransactionTableView.m
- (void)viewDidLoad
{
[super viewDidLoad];
dateFormatter = [self setDateFormatter];
[self.tableView registerNib:[UINib nibWithNibName:@"FEInsertUpdateVehicleDataCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"InsertUpdateVehicleDataCell"];
[self.tableView registerNib:[UINib nibWithNibName:@"FEInsertUpdateTransactionCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"FEInsertUpdateTransactionCell"];
//register tap
UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyboard)];
[self.tableView addGestureRecognizer:gestureRecognizer];
[self registerForKeyboardNotifications];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *dateTitle = @"Date";
static NSString *distance = @"KM";
static NSString *odometer = @"Odometer";
static NSString *unitPrice = @"Cost Per Liter";
static NSString *gasBought = @"Litters";
static NSString *CellIdentifierTransaction = @"FEInsertUpdateTransactionCell";
static NSString *CellIdentifier = @"InsertUpdateVehicleDataCell";
if ((indexPath.section == 0 ) || (indexPath.section == 2))
{
FEInsertUpdateTransactionCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifierTransaction];
//Configure Cell
if ((indexPath.row == 0) && (indexPath.section == 0))
{
[self configureTransactionCell:cell atIndexPath:indexPath withString:@"Select Vehicle"];
return cell;
}
else if ((indexPath.row == 0) && (indexPath.section == 2))
{
[self configureTransactionCell:cell atIndexPath:indexPath withString:@"Total Transaction"];
return cell;
}
else if ((indexPath.row == 1) && (indexPath.section == 2))
{
[self configureTransactionCell:cell atIndexPath:indexPath withString:@"Fuel Consumption"];
return cell;
}
}
else if (indexPath.section == 1)
{
FEInsertUpdateVehicleDataCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure Cell
if (indexPath.row == 0)
{
[self configureCell:cell atIndexPath:indexPath withString:dateTitle];
return cell;
}
else if (indexPath.row == 1)
{
[self configureCell:cell atIndexPath:indexPath withString:distance];
return cell;
}
else if (indexPath.row == 2)
{
[self configureCell:cell atIndexPath:indexPath withString:odometer];
return cell;
}
else if (indexPath.row == 3)
{
[self configureCell:cell atIndexPath:indexPath withString:gasBought];
return cell;
}
else
{
[self configureCell:cell atIndexPath:indexPath withString:unitPrice];
return cell;
}
}
FEInsertUpdateVehicleDataCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
return cell;
}
- (void)configureCell:(FEInsertUpdateVehicleDataCell *)cell atIndexPath:(NSIndexPath *)indexPath withString: (NSString *)title
{
dateFormatter = [self setDateFormatter];
if(title.length > 0)
{
cell.DescLabel.text = title;
}
cell.DescLabel.textColor = [UIColor blueColor];
cell.DetailTextField.textColor = [UIColor blackColor];
if (indexPath.row == 0)
{
if ((_isEdit) && !(_isUpdated))
{
cell.DetailTextField.text= [dateFormatter stringFromDate:_vehicleTransaction.tDate];
}
}
else if (indexPath.row == 1)
{
if ((_isEdit) && !(_isUpdated))
{
cell.DetailTextField.text = [NSString stringWithFormat:@"%@",_vehicleTransaction.tDistance];
;
}
}
else if (indexPath.row == 2)
{
if ((_isEdit) && !(_isUpdated))
{
cell.DetailTextField.text = [NSString stringWithFormat:@"%@",_vehicleTransaction.tOdometer];
}
}
else if (indexPath.row == 3)
{
if ((_isEdit) && !(_isUpdated))
{
cell.DetailTextField.text = [NSString stringWithFormat:@"%@",_vehicleTransaction.tFuelVolume];
}
}
else if (indexPath.row == 4)
{
if ((_isEdit) && !(_isUpdated))
{
cell.DetailTextField.text = [NSString stringWithFormat:@"%@",_vehicleTransaction.tFuelPrice];
}
}
cell.DetailTextField.tag = tags ;
NSLog(@"Title is %@ with tag: %ld",title,(long)cell.DetailTextField.tag);
tags++;
if ((cell.DetailTextField.text.length == 0) && !(_isUpdated))
{
if (title.length > 0)
{
if ((indexPath.section == 1 ) && (indexPath.row == 0))
{
cell.DetailTextField.textColor = [UIColor blackColor];
cell.DetailTextField.text = [NSString stringWithFormat:@"%@",
[dateFormatter stringFromDate:[NSDate date]]];
}
cell.DetailTextField.textColor = [UIColor grayColor];
cell.DetailTextField.placeholder = title;
}
}
if ((indexPath.section == 1 ) && (indexPath.row == 0) && (_ischoseDate))
{
cell.DetailTextField.text =[NSString stringWithFormat:@"%@",
[dateFormatter stringFromDate:_choosenDate]];
NSLog(@"Chosen Date is: %@",cell.DetailTextField.text);
}
dateFormatter = nil;
cell.DetailTextField.delegate = self;
}
- (void)configureTransactionCell:(FEInsertUpdateTransactionCell *)cell atIndexPath:(NSIndexPath *)indexPath withString: (NSString *)title
{
if(title.length > 0)
{
cell.TitleLabel.text = title;
}
cell.TitleLabel.textColor = [UIColor blueColor];
cell.DetailLabel.textColor = [UIColor blackColor];
if (indexPath.section == 0)
{
NSString *make = [_vehicleData.vMaker stringByAppendingString:@" "];
cell.DetailLabel.text = [make stringByAppendingString:_vehicleData.vModel];
}
if (indexPath.section == 2)
{
if (indexPath.row == 0 )
{
cell.DetailLabel.text = [NSString stringWithFormat:@"%@",_vehicleTransaction.tTotalAmmount];
}
else if (indexPath.row == 1)
{
cell.DetailLabel.text = [NSString stringWithFormat:@"%@",_vehicleTransaction.tFuelEconomy];
}
}
cell.DetailLabel.tag = tags ;
NSLog(@"Title is %@ with tag: %ld",title,(long)cell.DetailLabel.tag);
if (tags == 7)
{
tags = 0;
}
tags++;
if ((cell.DetailLabel.text.length == 0) && !(_isUpdated))
{
if (title.length > 0)
{
cell.DetailLabel.textColor = [UIColor blackColor];
cell.DetailLabel.text = @"0";
}
}
}
- (void)showDate
{
int height = 255;
//create new view
UIView * _newView = [[UIView alloc] initWithFrame:CGRectMake(0, 200, 320, height)];
_newView.backgroundColor = [UIColor colorWithWhite:1 alpha:1];
//add toolbar
UIToolbar * toolbar = [[UIToolbar alloc] initWithFrame: CGRectMake(0, 0, 320, 50)];
toolbar.barStyle = UIBarStyleBlack;
//add button
UIBarButtonItem *infoButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(DatePickerDoneClick:)];
toolbar.items = [NSArray arrayWithObjects:infoButtonItem, nil];
//add date picker
datePicker = [[UIDatePicker alloc] init];
datePicker.datePickerMode = UIDatePickerModeDate;
datePicker.hidden = NO;
datePicker.date = [NSDate date];
datePicker.frame = CGRectMake(0, 40, 320, 250);
[datePicker addTarget:self action:@selector(LabelChange:) forControlEvents:UIControlEventValueChanged];
[_newView addSubview:datePicker];
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(DatePickerDoneClick:)];
[tapRecognizer setNumberOfTapsRequired:1];
[tapRecognizer setDelegate:self];
[_newView addGestureRecognizer:tapRecognizer];
//add popup view
[_newView addSubview:toolbar];
[self.view addSubview:_newView];
//animate it onto the screen
CGRect temp = _newView.frame;
temp.origin.y = CGRectGetMaxY(self.view.bounds);
_newView.frame = temp;
[UIView beginAnimations:nil context:nil];
temp.origin.y -= height;
_newView.frame = temp;
[UIView commitAnimations];
}
-(IBAction)DatePickerDoneClick:(id *)test
{
NSLog(@"Done is pressed");
_choosenDate = datePicker.date;
[pickerView removeFromSuperview];
[datePicker removeFromSuperview];
//[_view removeFromSuperview];
//[_newView removeFromSuperview]; --> _newView is undeclared.
//[_newView removeFromSuperview];
//[actionSheet dismissWithClickedButtonIndex:0 animated:YES];
_ischoseDate = YES;
[self.tableView reloadData];
}
- (void)LabelChange:(id)sender
{
_choosenDate = datePicker.date;
}
Thanks all for your help