Hello i’m trying to make a simple ios app with tabs and navigation .
in the delegate i have the following type:
BlogRss * _currentlySelectedBlogItem;
with this property:
@property (readwrite,retain) BlogRss * currentlySelectedBlogItem;
and i’m trying to get his data with two other classes, one is a table view with the data and the other will show the data;
in both classes i have declared the following:
@class NewsAppDelegate;
NewsAppDelegate * _appDelegate;
@property (nonatomic, retain) IBOutlet _NewsAppDelegate * appDelegate;
@synthesize appDelegate = _appDelegate;
ofter “touching” the cell in the table view i wrote this:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[[self appDelegate] setCurrentlySelectedBlogItem:[[[self rssParser]rssItems]objectAtIndex:indexPath.row]];
// Navigation logic may go here. Create and push another view controller.
// [[self appDelegate] loadNewsDetails];
NewsDetailViewController *detailViewController = [[NewsDetailViewController alloc] initWithNibName:@"NewsDetailViewController" bundle:nil];
// ...
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
}
when i’m writing in the other class:
NSLog(@"%@",self.appDelegate.currentlySelectedBlogItem);
i’m getting null.
clearly i’m doing something wrong, but i don’t know what…
The problem is likely that this line is returning null:
Code is easier to debug when you don’t nest so many sentences.
You can access your delegate from anywhere because UIApplication is a singleton, you don’t need to keep a reference as you do with
self.appDelegate. Example:Or just do as Jennis suggest, who is a faster typer than me, and remove the IBOutlet. 😛
When you do
The result is the same if you skip the first line, because the runtime creates it for you.
See Question about @synthesize. And I guess _NewsAppDelegate is really NewsAppDelegate (no underscore).