1

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"]];


}
7eleven
  • 15
  • 4

3 Answers3

2

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.

Brian Walker
  • 8,658
  • 2
  • 33
  • 35
  • ok i already try to use a delegate but never work. can u see my edits? thx – 7eleven May 22 '14 at 23:57
  • Where are you setting the delegate in the first view controller. If the delegate is nil it won't call it. You need to setup the delegate in the new view controller in the prepareForSegue callback that occurs in the first view controller: see [here](http://stackoverflow.com/questions/7864371/ios-how-to-pass-prepareforsegue-an-object). – Brian Walker May 23 '14 at 13:32
0

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.

JScarry
  • 1,507
  • 1
  • 13
  • 25
  • This is OK in his code because we did a explicit "@synthesize testArray" which creates the ivar as "testArray". If the synthesize was not there then the compiler would create the ivar as "_testArray" and there would be a problem. – Brian Walker May 22 '14 at 22:53
  • Since you synthesized it you need to use self. to access it. Otherwise you are creating a new variable with local scope. – JScarry May 23 '14 at 14:02
  • When you synthesize the property getter/setter are created and an ivar is created to hold the value. If an explicit "@synthesize testArray" is done the ivar is "testArray" and the code he has will work. If you leave out the @synthesize the compiler implicitly creates the ivar as "_testArray". I performed both steps in Xcode 5.1.1 and these are the results I saw. – Brian Walker May 23 '14 at 14:12
0

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
}
Kujey
  • 1,122
  • 6
  • 17
  • The issue is that CaisseViewController was created first and segued to the second view controller. The didSelectRowAtIndexPath is creating a second copy (that does not execute viewDidLoad). Even if it did there would be two instances of CaisseViewController and thus two instances of testArray which is not what I think is wanted here. – Brian Walker May 22 '14 at 22:55