- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog((@"This is didSelectRowAtIndexPAth"));
DetailViewController *detailViewController = [[DetailViewController alloc] init];
detailViewController.myDictionary = [self.placeArray objectAtIndex:[indexPath row]];
/* The below NSlog is showing the content , its not null still in next viewcontroller same variable showing null content*/
NSLog(@"My Dictionary: This is a MasterView%@",detailViewController.myDictionary);
[self performSegueWithIdentifier:@"showDetail" sender:self];
}
7 Answers
It is because the presented instance of your DetailViewController
is different than the instance you are creating manually. You should either present that new ViewController
yourself (modally or by push), or implement the prepareForSegue
method and set the dictionary there.
Edit:
The 2 solutions:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
DetailViewController *detailViewController = [[DetailViewController alloc] init];
detailViewController.myDictionary = [self.placeArray objectAtIndex:[indexPath row]];
// If it is a modal
[self presentViewController:detailViewController animated:YES];
}
OR
@implementation MyClass {
NSIndexPath *selectedCellIndexPath;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog((@"This is didSelectRowAtIndexPAth"));
selectedCellIndexPath = indexPath;
[self performSegueWithIdentifier:@"showDetail" sender:self];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"showDetail"]) {
DetailViewController *detailViewController = (DetailViewController *)segue.destinationViewController;
detailViewController.myDictionary = [self.placeArray objectAtIndex:selectedCellIndexPath.row];
}
}

- 7,313
- 2
- 32
- 44
-
Can you please elaborate your solution ? – Vishal Nov 11 '13 at 10:41
In DetailViewController check if the myDictionary is a property declared as weak or strong..

- 929
- 7
- 25
-
it was nontomic , retain , i changed it into strong .. Still no view Controller . – Vishal Nov 11 '13 at 10:54
try this
Decalre one NSInteger varable Globaly assign that varble to indexpath.row
@interface DashbordViewController ()
{
NSInteger SelectedRownum;
}
@end
@implementation DashbordViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
SelectedRownum=indexPath.row
[self performSegueWithIdentifier:@"showDetail" sender:self];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"showDetail"])
{
NSLog((@"This is didSelectRowAtIndexPAth%@",SelectedRownum));
DetailViewController *detailViewController = [segue destinationViewController];
detailViewController.myDictionary = [self.placeArray objectAtIndex:SelectedRownum];
}
}

- 1,044
- 10
- 17
-
Where is the prepareForSegue method getting the indexPath variable from? – Abizern Feb 21 '14 at 10:37
try this . . .
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog((@"This is didSelectRowAtIndexPAth"));
UIStoryboard *mystoryboard = [UIStoryboard storyboardWithName:@"myStoryBoardName" bundle:nil];
self.detailsViewController = [mystoryboard instantiateViewControllerWithIdentifier:@"detailviewcontroller"]
/* The below NSlog is showing the content , its not null still in next viewcontroller same variable showing null content*/
NSLog(@"My Dictionary: This is a MasterView%@",detailViewController.myDictionary);
[self performSegueWithIdentifier:@"showDetail" sender:self];
}
you have to set identifier of your detailViewController in storyBoard .

- 11,204
- 7
- 53
- 70
You need to assign the dictionary in prepareForSegue: method.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { DetailViewController *viewController = = (DetailViewController *)[segue destinationViewController]; detailViewController.myDictionary = //YOUR Dictionary here; }
This way the same dictionary can be obtained in viewDidLoad: of DetailViewController.
Note: Make sure you allocate the dictionary.
Hope it helps.

- 4,904
- 3
- 24
- 39
-
OMG. Why?!!! I cant pass dict like this: `dvc.dataArguments = [NSDictionary dictionaryWithObject:self.selectedDate forKey:kDateKey];` or like this: `NSDictionary *arguments = @{self.selectedDate : kDateKey}; viewController.dataArguments = arguments;`. Instead i need to do this: `dvc.dataArguments = [[NSDictionary alloc] initWithObjectsAndKeys:self.selectedDate, kDateKey, nil];` to get data in `- (void) viewWillAppear:(BOOL)animated`. Ahhhh... – Martin Berger Jul 18 '15 at 20:10
Since I can't see implementation of your controller (being subclass of UIViewController or not), I cannot say a lot, but keep in mind that initWithNibName:bundle:
is the designated initializer for UIViewController and not init
.
So, this is the first problem. In any case, check the instance before setting a property. It is very likely that instance is nil.
EDIT:
It is also very likely, they you are instantiating new View Controller from existing. In this case, you can add new view controller as child controller or present new controller from existing. In both cases properties of existing are reachable from new through properties: parentViewController
and presentingViewController
, which makes the story opposite: you take property value from existing controller into new controller.

- 2,460
- 3
- 22
- 37
-
-
If you have NIB file associated with your DetailViewController, then init with this NIB name and bundle with nil. Please, do read Apple Developer information about UIViewController class and https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/Introduction/Introduction.html#//apple_ref/doc/uid/TP40007457. – mbpro Nov 11 '13 at 11:30
-
-
Then you shouldn't instantiate View Controller yourself. It is done by Storyboard. Look at the answer from @Meet. And again: read developer documentation about View Controllers and Storyboards. – mbpro Nov 11 '13 at 11:42
You are creating new view controller in didSelectRowAtIndexPath
, and at the end you are calling prepareForSegue
method... which will create new Instance, So you have to set your data which you want to send in new view controller in prepareForSegue
method like this.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
//
if ([[segue identifier] isEqualToString:@"name_of_ur_segue"])
{
// Get reference to the destination view controller
DetailViewController *detailViewController = [segue destinationViewController];
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
// Pass any data here ...
detailViewController.myDictionary = [self.placeArray objectAtIndex:[indexPath row]];
}
}
Edit:
As you are accessing data in ViewDidLoad method and in iOS 7 data passed like this is not accessible in ViewDidLoad method. As view controller presentation logic is bit changed now. Now we can pass data in initMethod if we want to access this in viewDidLoad
.
Or access this in viewWillAppear
or viewDidApepar
.
If you want to call this code only once in viewDidAppear or ViewWillApepar just set a boolean flag, which can tell if you want to access this or not.

- 14,377
- 4
- 45
- 54
-
Its not working , just can see the data in NSlog of the same viewController . – Vishal Nov 11 '13 at 10:48
-
Show some code from detailViewController, So I can see what are you doing with this data and where. Actually in iOS 7 if you will acceess data which is passed like this in ViewDidLoad methodyou will get nil response. You have to access that data after viewDidLoad in viewWillAppear or ViewDidAppear... – Adnan Aftab Nov 11 '13 at 10:50
-
-