Pass an object defined from a customed tableViewCell - ios

To explain my situation, I have a tableView with custom tableViewCell and I have created a button in this customed cell. I want this button to add an object box which is already defined to an NSMutableArray and pass this array to restoCardConfirmationViewController in order to create a cart.
The problem is that when I try to display the NSMutablearray in restoCardConfirmationViewController, the result is nil. This is my code :
The boxTableViewCell :
#import "boxTableViewCell.h"
#import "RestoCardConfirmationViewController.h"
#import "RestauCardViewController.h"
#implementation boxTableViewCell {
NSMutableArray *_pickerPlace;
}
- (IBAction)select:(id)sender {
RestoCardConfirmationViewController *restauCardConfirmation = [[RestoCardConfirmationViewController alloc] init];
[restauCardConfirmation.boxesCommande addObject:_box];
NSLog(#"la box choisie est %#",_box);
self.select.enabled = NO;
if(!self.select.enabled){
NSLog(#"button desabled");
}
}
The restoCardConfirmationViewController.h :
#import <UIKit/UIKit.h>
#import <Parse/Parse.h>
#interface RestoCardConfirmationViewController : UIViewController
#property(nonatomic) NSMutableArray *boxesCommande;
The restoCardConfirmationViewController.m :
#import "RestoCardConfirmationViewController.h"
#import "ChoisirDateViewController.h"
#interface RestoCardConfirmationViewController ()
#end
#implementation RestoCardConfirmationViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(#"The NSMutableArray in the other %#",self.boxesCommande);
// Do any additional setup after loading the view.
}

One approach is Delegation. Create a delegate that notifies every object added to RestoCardConfirmationViewController.
And the other simpler method is :
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.purchases count];
}
In the MainViewViewController, add button click event instead of using it in tableview cell.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
cell.button.tag = indexPath.row;
[cell.button addTarget:self
action:#selector(select)
forControlEvents:UIControlEventTouchUpInside];
// ...
}
This calls the following method, also implemented in MainViewController
- (IBAction)select:(id)sender {
int index = ((UIButton *)sender).tag;//it returns the indexPath
[self.boxesArray addObject: self.purchases[index]];
NSLog(#"la box choisie est %#",_box);
self.select.enabled = NO;
if(!self.select.enabled){
NSLog(#"button desabled");
}
}
Now in prepareforSeque, pass the created array to RestoCardConfirmationViewController.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Make sure your segue name in storyboard is the same as this line
if ([[segue identifier] isEqualToString:#"YOUR_SEGUE_NAME_HERE"])
{
// Get reference to the destination view controller
RestoCardConfirmationViewController *vc = [segue destinationViewController];
// Pass any objects to the view controller here, like...
vc.boxesCommande = self.boxesArray;
}
}

Push To ViewController After addObject :
- (IBAction)select:(id)sender
{
RestoCardConfirmationViewController *restauCardConfirmation = [[RestoCardConfirmationViewController alloc] init];
[restauCardConfirmation.boxesCommande addObject:_box];
NSLog(#"la box choisie est %#",_box);
self.select.enabled = NO;
if(!self.select.enabled)
{
NSLog(#"button desabled");
}
[self.navigationController pushViewController:restauCardConfirmation animated:YES];
}

Don't forget to initialize your boxesCommande array in your custom view controller.
Add the following lines to your RestoCardConfirmationViewController.m in the #implementation:
- (instancetype)init {
if (self = [super init]) {
self.boxesCommande = [[NSMutableArray alloc] init];
}
return self;
}

Related

how to add and save data to an array then in obj c?

So Im doing a little mini project, what Im trying to do is create something similar to a note taking app. What im doing is allowing the user to type in the title of their note in a text field, then adding that to a UITableViewCell, then when they click on that cell it takes them to another view where they can edit their title, save it and then return them to the original view but with a different title then the one they originally entered.
Here are my current issues
1) How do I save the new titles after the app is closed? (When I add a new title then restart the simulator the UITableView is empty)
2) How do I permanently change the UITableViewCell once the user edits the title? (The titles text goes into the text field but when I edit the title and hit save and go back to the original view controller the original title is still there)
Here is my code for the first view controller:
#import "TestViewController.h"
#interface TestViewController (){
}
#end
#implementation TestViewController
- (void)viewDidLoad {
[super viewDidLoad];
_array = [[NSMutableArray alloc] initWithObjects:#"First Note", nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)btn:(id)sender {
[_array insertObject:self.fld.text atIndex:0];
[_tableView reloadData];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [_array count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell" forIndexPath:indexPath];
if (cell == nil){
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"cell"];
}
cell.textLabel.text = [_array objectAtIndex:indexPath.row];
return cell;
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([segue.identifier isEqualToString:#"segue1"]){
Test1ViewController *vc = [segue destinationViewController];
Connecter *connectorClass = [[Connecter alloc] init];
connectorClass.stringToPass = self.array[self.tableView.indexPathForSelectedRow.row];
vc.connectorClass = connectorClass;
}
}
Heres my code for the second view controller:
#import "Test1ViewController.h"
#interface Test1ViewController ()
#end
#implementation Test1ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
_label.text = _connectorClass.stringToPass1;
_textView.text = _connectorClass.stringToPass;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)button1:(id)sender {
[self performSegueWithIdentifier:#"segue2" sender:self];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"segue2"]){
Test2ViewController *vc1 = [segue destinationViewController];
Connecter *connectorClass = [[Connecter alloc] init];
connectorClass.stringToPass1 = _textView.text;
vc1.connectorClass = connectorClass;
}
}
And here is a third view controller I used as a middle man to pass information through from the testviewcontroller to the test1viewcontroller
#import <UIKit/UIKit.h>
#interface Connecter : UIViewController
#property (nonatomic, strong) Connecter *connectorClass;
#property (nonatomic, strong) NSString *stringToPass;
#property (nonatomic, strong) NSString *stringToPass1;
#end

how to set value to textField through a delegate

i have two view controllers, view controller and tableViewController.
ViewController contains a textField and a button. when the button in viewController is clicked, then TableViewController should start.
in TableViewController, there is a list contains 5 rows, each row contains a string value.
What I am trying to do is, hen the user chooses any row in the list in TableViewController, then ViewController scene should start and the value that the user chose should be displayed in the textfield in ViewController.
to achieve this task, I created a protocol in TableViewController with a required method called valueChoosen: (NSString *) value, and this method is implemented in ViewController, and then I set the value to the textField but, at run time the TextField is empty.
please have look at the code below, and let me know why the value passed to the required method of the protocol can not be set to the textfield, what I am doing wrong;
TableViewController:
#import "TableViewController.h"
#interface TableViewController ()
#property NSArray *tableData;
#end
#implementation TableViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.tableData = [NSArray arrayWithObjects:#"Android",
#"iOS",
#"swift",
#"objective-c",
#"openCV",nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(NSInteger) tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section {
return [self.tableData count];
}
-(UITableViewCell *) tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *simpleTableIdentifier = #"SimpleTableItem";
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:simpleTableIdentifier];
}
cell.textLabel.text = [self.tableData objectAtIndex:indexPath.row];
cell.imageView.image = [UIImage imageNamed:#"images.jpg"];
return cell;
}
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:
(NSIndexPath *)indexPath {
NSLog(#"%#", [self.tableData objectAtIndex:indexPath.row]);
[self performSegueWithIdentifier:#"unwindSegue" sender:NULL];
[self.delegate valueChoosen:[self.tableData
objectAtIndex:indexPath.row]];
}
#end
ViewController:
#import "ViewController.h"
#import "TableViewController.h"
#interface ViewController ()
#property (strong, nonatomic) IBOutlet UITextField
*textFieldDepartment;
#property (strong, nonatomic) IBOutlet UIButton
*buttonSelectFromTableView;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a
nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"segueToTableView"]) {
//code
TableViewController *table = segue.destinationViewController;
table.delegate = self;
}
}
-(void) valueChoosen: (NSString *) value {
[_textFieldDepartment setText:value];
NSLog(#"value: %#", value);
}
#end
It maybe because you are setting the textfield text in the viewController which is not being displayed. Try assigning the value to a variable using the delegate function and call the delegate function before performing the unwind segue. And on the viewController try setting the textfield text as the variable whose value was set through delegate method from the viewWillAppear method.
Step 1:
create the unwind action in the viewcontroller you want to unwind it.in your case it is ViewController
- (IBAction)unwindFromModalController:(UIStoryboardSegue *)segue{}
Step 2:
Connect to the unwind Action
Control drag from tableviewcell to the Exit sign.
Select the Action you specified.
Add the Identifier to unwind segue.
Step3
Trigger the segue
in your TableViewController.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
self.selectedText = self.tableData[indexPath.row];
[self performSegueWithIdentifier:#"unwindSegue" sender:self];}
Step 4:
Pass Data
Unwind Action
- (IBAction)unwindFromModalController:(UIStoryboardSegue *)segue{
if ([segue.sourceViewController isKindOfClass:[TableViewController class]]) {
TableViewController *vc = segue.sourceViewController;
if (vc.selectedText) {
[_textFieldDepartment setText:vc.selectedText];}
}
}

Add object to an NSArray from UITableViewCell

I have created a customed UITableViewCell that contains a PFObject property and a button. I want the button to add this object on a NSMutableArray and pass this Array to another UIViewController. The problem is that I can't implement the prepareForSegue method into custom UITableViewCell so when I display the array in restoCardConfirmationVC I always get an empty array.
This is my code :
#import "boxTableViewCell.h"
#import "RestoCardConfirmationViewController.h"
#import "RestauCardViewController.h"
#implementation boxTableViewCell {
NSMutableArray *_pickerPlace;
RestauCardViewController *_restoCardVC;
RestoCardConfirmationViewController *_restoCardConfirmationVC;
}
- (void)awakeFromNib {
// Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return _pickerPlace.count;
}
- (NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [_pickerPlace objectAtIndex:row];
}
- (IBAction)select:(id)sender {
NSLog(#"the select box is : %#",_box);
[_restoCardConfirmationVC.boxesCommande addObject:_box];
self.select.enabled = NO;
}
#end
RestaCardConfirmation.h
#import <UIKit/UIKit.h>
#import <Parse/Parse.h>
#interface RestoCardConfirmationViewController : UIViewController
#property(nonatomic) PFObject *commande;
#property(nonatomic) NSMutableArray *boxesCommande;
- (IBAction)confirmer:(id)sender;
#end
You should initialise the array before using it. Like this
- (void)awakeFromNib {
// Initialization code
_restoCardConfirmationVC = [[RestoCardConfirmationViewController alloc] init];
_restoCardConfirmationVC.boxesCommande = [NSMutableArray array];
}
Although this is a bad approach. You should initialise the array in the RestoCardConfirmationViewController initialisation.
You have not initialised the Array in which you are adding object. Allocate it either in awakeFromNib or in ViewDidLoad and then add an object in it.
Moreover, I don't find a good reason to alloc view controller in awakeFromNib of TableViewCell. You should allocate the VC only when Button is Tapped.
Hence the code should be like:
- (IBAction)select:(id)sender {
_restoCardConfirmationVC = [[RestoCardConfirmationViewController alloc] init];
[_restoCardConfirmationVC view]; //This will call the ViewDidLoad in Advance
[_restoCardConfirmationVC.boxesCommande addObject:_box];
self.select.enabled = NO;
}
And in ViewDidLoad of RestoCardConfirmationViewController:
- (void)viewDidLoad {
[super viewDidLoad];
_boxesCommande = [NSMutableArray array];
//Other initialisation code
}

How to display data from master view into detail view controller

I am new to Ios programming i want to display the data from master view controller into the detail view controller, i am using the master detail template from xcode. the detail array is working it shows the detail in label, how ever i want to display the title as well which is stored in _objects array,
here is my code
#import "MasterViewController.h"
#import "DetailViewController.h"
#interface MasterViewController () {
NSMutableArray *_detailObjects;
NSMutableArray *_thumbnailImage;
}
#end
#implementation MasterViewController
#synthesize _objects;
- (void)awakeFromNib
{
[super awakeFromNib];
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.title= #"Zodiac List";
_objects = [[NSMutableArray alloc] initWithObjects:
#"value 1",
#"value 2",
#"value 3",
#"value 4",
#"value 5",
,nil];
_detailObjects = [[NSMutableArray alloc] initWithObjects:
#"detail of value 1",
#"detail of value 2",
#"detail of value 3",
#"detail of value 4",
#"detail of value 5",
, nil];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return _objects.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
NSDate *object = _objects[indexPath.row];
cell.textLabel.text = [object description];
return cell;
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"showDetail"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
NSMutableArray *object = _detailObjects[indexPath.row];
[[segue destinationViewController] setDetailItem:object];
}
}
#end
the detail is working fine how ever i want to display the value 1, value 2,value 2 in a label as well..
here is my detail view controller code..
#property (strong, nonatomic) id detailItem;
#property (strong, nonatomic) id detailItemTitle;
#property (weak, nonatomic) IBOutlet UILabel *detailDescriptionLabel;
#property (weak, nonatomic) IBOutlet UILabel *zodiacNameLabel;
here is .m file code
#import "DetailViewController.h"
#import "MasterViewController.h"
#interface DetailViewController ()
- (void)configureView;
#end
#implementation DetailViewController
#synthesize zodiacNameLabel;
#pragma mark - Managing the detail item
- (void)setDetailItem:(id)newDetailItem
{
if (_detailItem != newDetailItem) {
_detailItem = newDetailItem;
// Update the view.
[self configureView];
}
}
-(void) setDetailItemTitle:(id)detailItemTitle{
if (_detailItemTitle !=detailItemTitle) {
_detailItemTitle = detailItemTitle;
[self configureView];
}
}
- (void)configureView
{
// Update the user interface for the detail item.
if (self.detailItem) {
self.detailDescriptionLabel.text = [self.detailItem description];
// self.zodiacNameLabel.text = [self.]
MasterViewController *master = [[MasterViewController alloc] init];
zodiacNameLabel.text = [master._objects objectAtIndex:0];
}
// if (self.detailItemTitle) {
// self.zodiacNameLabel.text = [self.detailItem description];
// }
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self configureView];
// MasterViewController *master = [[MasterViewController alloc] init];
// zodiacNameLabel.text = [master._objects objectAtIndex:0];
// MasterViewController *master = [[MasterViewController alloc]init];
// zodiacNameLabel.text = [master._objects objectAtIndex:0]; // you can get first element of an array
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
Implement delegate method of tableview which is in your masterviewcontroller and then inside that push your data whatever you want to display in masterviewcontroller like that:-
Assuming that below delegate is in your masterViewController
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
_yourController = [[yourController alloc] initWithNibName:#"yourController" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:_yourController animated:YES];
}
Now this will alloc, initialize and load your nib into detailviewcontroller and if you want to send message from master to detail then declare property method in detailviewcontroller and just pass it from above like that below:-
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
_yourDetailController = [[yourDetailController alloc] initWithNibName:#"yourDetailController" bundle:[NSBundle mainBundle]];
[_yourDetailController setTempString:#"test"]; //Assuming that tempString is declared in yourDetailController class
[self.navigationController pushViewController:_yourDetailController animated:YES];
}
Make an array that is "tmpArray" in detailViewController and then you can copied _objects array in to tmpArray. like this
MastrerViewController.m
DetailViewController *detail = [[DetailViewController alloc]init];
detail.tmpArray = [[NSMutableArray alloc] initWithArray:_objects];
DetailViewController.h
#property (nonatomic,retain) NSMutableArray *tmpArray;

TableviewController delegate not returning value

I have a tableview controller that is a static table used for request data. When someone selects a row, it displays a new tableview controller with options using a modal segue, the user selects a option and then presses the 'hecho' (done) button and the value should be returned to the first table view controller, but it just not happening. If I inspect the delegate it just says null. What could I be doing wrong?
The story board
First table
AgregarCitaTableViewController.h
#import <UIKit/UIKit.h>
#import "SeleccionarPacienteTableViewController.h"
#interface AgregarCitaTableViewController : UITableViewController <SeleccionarPacienteTableViewControllerDelegate>
#end
AgregarCitaTableViewController.m
#import "AgregarCitaTableViewController.h"
#import "SeleccionarPacienteTableViewController.h"
#interface AgregarCitaTableViewController ()
{
NSDictionary *datosPaciente;
}
#end
#implementation AgregarCitaTableViewController
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)paciente:(NSDictionary *)paciente
{
datosPaciente = paciente;
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
cell.detailTextLabel.text = [datosPaciente objectForKey:#"nombre"];
}
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if( [segue.identifier isEqualToString:#"listaPacientes"] )
{
SeleccionarPacienteTableViewController *viewController = segue.destinationViewController;
viewController.delegate = self;
}
}
Second Table
SeleccionarPacienteTableViewController.h
#protocol SeleccionarPacienteTableViewControllerDelegate <NSObject>
-(void)paciente:(NSDictionary *)paciente;
#end
#interface SeleccionarPacienteTableViewController : UITableViewController
{
id delegate;
}
#property(nonatomic,assign)id<SeleccionarPacienteTableViewControllerDelegate> delegate;
#end
SeleccionarPacienteTableViewController.m
#import "SeleccionarPacienteTableViewController.h"
#interface SeleccionarPacienteTableViewController ()
{
NSMutableArray *todosPacientes;
NSDictionary *paciente;
NSInteger checkmarkedRow;
}
#end
#implementation SeleccionarPacienteTableViewController
#synthesize delegate = _delegate;
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
UIBarButtonItem *hecho = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(hecho)];
[[self navigationItem] setRightBarButtonItem:hecho];
UIBarButtonItem *cancelar = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector(cancelar)];
[[self navigationItem] setLeftBarButtonItem:cancelar];
//Llamada asincrona, cargar las citas
[self performSelectorInBackground:#selector(cargarDatos) withObject:nil];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
#warning Potentially incomplete method implementation.
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
#warning Incomplete method implementation.
// Return the number of rows in the section.
return todosPacientes.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// Configure the cell...
NSDictionary *object;
object = todosPacientes[indexPath.row];
cell.textLabel.text = [object objectForKey:#"nombre"];
if(checkmarkedRow == indexPath.row){
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//Guardar nombre del paciente selccionado
paciente = todosPacientes[indexPath.row];
// In cellForRow... we check this variable to decide where we put the checkmark
checkmarkedRow = indexPath.row;
// We reload the table view and the selected row will be checkmarked
[tableView reloadData];
// We select the row without animation to simulate that nothing happened here :)
[tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
// We deselect the row with animation
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
- (void) cargarDatos
{
//Path donde se encuentra el plist con los datos
NSString *path = [[NSBundle mainBundle]pathForResource:#"pacientes" ofType:#"plist"];
//Guardamos las citas en un NSMutableArray
todosPacientes = [[NSMutableArray alloc]initWithContentsOfFile:path];
}
- (void) hecho
{
/*Is anyone listening
if([delegate respondsToSelector:#selector(paciente:)])
{
//send the delegate function with the amount entered by the user
[delegate paciente:paciente];
}*/
[self.delegate paciente:paciente];
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void) cancelar
{
[self dismissViewControllerAnimated:YES completion:nil];
}
#end
Solution:
Im not really sure how it worked or even if this is the real solution. I used the anwer of Hani Ibrahim and it didn't work but what he said was right. Then i just changed [delegate paciente:paciente] in the hecho method for [self.delegate paciente:paciente] and it worked.
I hope this can help someone.
The problem is that your seque display UINavigationController and not SeleccionarPacienteTableViewController
Change this method
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if( [segue.identifier isEqualToString:#"listaPacientes"] )
{
SeleccionarPacienteTableViewController *viewController = segue.destinationViewController;
viewController.delegate = self;
}
}
to be
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if( [segue.identifier isEqualToString:#"listaPacientes"] )
{
UINavigationController *navController = segue.destinationViewController;
SeleccionarPacienteTableViewController *viewController = [navController.viewControllers objectAtIndex:0];
viewController.delegate = self;
}
}
You have another problem
When doing this
#property(nonatomic,assign)id<SeleccionarPacienteTableViewControllerDelegate> delegate;
and this
#synthesize delegate = _delegate;
Then Your property will work with an instance variable called '_delegate' as you are saying #synthesize delegate = _delegate;
However this code
#interface SeleccionarPacienteTableViewController : UITableViewController
{
id delegate;
}
defines a new instance variable called delegate which is completely different than your property
So to access your property you can use self.delegate or _delegate and NOT delegate as it is another instance variable !
I think the problem is in prepareForSegue. The destinationViewController is a UINavigationController and not a SeleccionarPacienteTableViewController.
You have to access the first child of the destinationViewController to get your SeleccionarPacienteTableViewController.
Replacing segue.destinationViewController with [segue.destinationViewController topViewController] will probably solve your problem.

Resources