Reputation: 15
Hi this is my first question
look my code. I dont understand why my array becomes null after changing view FirstView: I try tu use a delegate but it's not working. my delegate is never call.
h:
@interface CaisseViewController : UIViewController <testDelegate>
@property (nonatomic, retain) NSMutableArray *testArray;
-(void)autrePaiementChoisie:(AutrePaiementCaisseTableViewController*)controller selectPaiement:(NSString *)paiement;
@end
m:
@synthesize testArray;
- (void)viewDidLoad
{
testArray = [[NSMutableArray alloc]initWithObjects:@"TEST 1",@"TEST 2", nil];
}
- (IBAction)autre:(id)sender {
NSLog(@"testArray %@",testArray); // OK !
[self performSegueWithIdentifier:@"autre" sender:self];
}
-(void)autrePaiementChoisie:(AutrePaiementCaisseTableViewController*)controller selectPaiement:(NSString *)paiement {
[controller dismissViewControllerAnimated:YES completion:nil];
NSLog(@"TEST ARRAY %@",testArray); // Is NULL
}
//function of my delegate
-(void)sendString:(NSString *)aString {
NSLog(@"string %@",aString); //dont work ! never called
}
SecondView: declare the delegate h:
@protocol testDelegate <NSObject>
-(void)sendString:(NSString*)aString;
@end
@property (nonatomic,assign) id<testDelegate>delegate;
m:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSDictionary *paiement = [self.paiementArray objectAtIndex:indexPath.row];
[self.delegate sendString:[paiement valueForKey:@"nom"]]; //talk with my delegate
// CaisseViewController *caisseView = [[CaisseViewController alloc]init];
//[caisseView autrePaiementChoisie:self selectPaiement:[paiement valueForKey:@"nom"]];
}
Upvotes: 1
Views: 301
Reputation: 1122
Here :
CaisseViewController *caisseView = [[CaisseViewController alloc]init];
[caisseView autrePaiementChoisie:self selectPaiement:[paiement valueForKey:@"nom"]];
You instantiated a CaisseViewController and call its method (autrePaiementChoisie:selectPaiement:) even before the viewdidload. Your testArray isn't instantiated at the time you print it. Maybe you should use 'lazy loading' for your testArray instead of initializing it in viewDidLoad.
Here is how it works, put this code in the firstVC.m :
-(NSMutableArray *) testArray
{
if (!_testArray) {
_testArray = [[NSMutableArray alloc]initWithObjects:@"TEST 1",@"TEST 2", nil];
}
return _testArray
}
Upvotes: 0
Reputation: 8928
The reason testArray is nil is that in the second view controller you are creating a second instance of CaisseViewController in didSelectRowAtIndexPath which does has not executed viewDidLoad (and even if it did you would have a separate copy of the testArray).
As part of loading and moving to the second view controller you should pass the instance of the first view controller to it (saving it as a weak pointer like a delegate). You can use that instance in the second view controller's didSelectRowAtIndexPath to call back to the first one.
Upvotes: 2
Reputation: 1507
Your code should be throwing an error for this line.
testArray = [[NSMutableArray ...
You haven’t defined testArray in this method and are assigning something to it. Try changing testArray to self. testArray in all of your code.
Upvotes: 0