1

I am pushing a string from one UIViewController to another so far I am doing this successfully however, when I add the pushed string to an NSMutableArray and NSLog the count of the array it always returns me 0 here is my code: I am using storyboards and do I need to reference the second view into the view did load?

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    [_TableView setDelegate:self];
    //[_TableView setDataSource:self];
    _numberOfRows = [[NSMutableArray alloc]init];
}

-(void)pushName:(NSString*)contactName
{
    //NSLog(@"Pushed name is: %@ ", contactName);
    _contactName = contactName;
    NSLog(@"Pushed name is: %@ ", _contactName);//<-- Returns the pushed string fine

    [_numberOfRows addObject:contactName];
    //[self.TableView reloadData];
     NSLog(@"Number of names: %d ", [_numberOfRows count]);//<--Always returns a 0

} 

Not really sure why this isn't working. If more code is required to clarify this question, then please do ask.

arled
  • 2,577
  • 6
  • 27
  • 49
  • take a look here http://stackoverflow.com/a/16142163/1328096 – jamil May 29 '13 at 14:58
  • @Undo No this is not a duplicate question I am not passing an NSMutableArray im passing a single string I just want to populate an array in the second UIViewController they are totally different questions bro – arled May 29 '13 at 15:04
  • @one Same principle. Anything that passes an array can also pass a string. Just go through the answers and replace `NSMutableArray` with `NSString`. – Undo May 29 '13 at 15:06
  • @Undo If you look at my code I am passing my string and it works but my issue is to populate the NSMutable array with the string passed. Although I am looking at the other question and it is totally different based on what I am asking for. I have managed to do it for the iPad but this one is proving to be tricky. I would appreciate it a lot if you could remove my down vote no need for it bro – arled May 29 '13 at 15:10
  • What is the response of Pushed name log? – Dilip Manek May 29 '13 at 15:12
  • @Dilip the response seems to be always 0 – arled May 29 '13 at 15:13
  • Not the array count but first log? – Dilip Manek May 29 '13 at 15:14
  • `NSLog(@"Pushed name is: %@ ", _contactName);` response of this code? – Dilip Manek May 29 '13 at 15:14
  • @Dilip the first log gives me the textfield string passed from the second UIViewController )the first log works fine – arled May 29 '13 at 15:15
  • might help to see some of your variable declarations – Chris Tetreault May 29 '13 at 15:40

1 Answers1

1

Your array seems to not be instantiated yet when you call pushName.

Can you provide us with the context of where you call it?

EDIT: Just to elaborate. Your problem isn't in passing the data between the two Controllers. Your problem seems to be that your Array isn't responding to the addObject and count messages.

Try using lazy instantiation on your array and it should work.

EDIT 2: Code snippet: Try adding the method:

- (NSMutableArray *)numberOfRows:
{
  if( _numberOfRows == nil ) _numberOfRows = [[NSMutableArray alloc] init]
  return _numberOfRows
}

And where you use _numberOfRows try using self.numberOfRows, i.e.

[self.numberOfRows addObject:contactName];
//[self.TableView reloadData];
NSLog(@"Number of names: %d ", [self.numberOfRows count]);//<--Should return 1 or more now.

EDIT 3: Oh and if you do that you can remove the instantiation on viewDidLoad. But you knew that.

EDIT 4: Fixing up the code snippets. Sorry I edit so much. Just trying to make sure you get the right answer.

Murillo
  • 1,043
  • 8
  • 9