开发者

Access Delegate Data

开发者 https://www.devze.com 2023-03-12 07:00 出处:网络
Hello i\'m trying to make a simple ios app with tabs and navigation . in the delegate i have the following type:

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:@"NewsDetailViewControl开发者_StackOverflowler" 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:

[[[self rssParser]rssItems]objectAtIndex:indexPath.row]

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:

(NewsAppDelegate *)[[UIApplication sharedApplication] delegate];

Or just do as Jennis suggest, who is a faster typer than me, and remove the IBOutlet. :P

When you do

NewsAppDelegate * _appDelegate;
@property (nonatomic, retain) IBOutlet _NewsAppDelegate * appDelegate;
@synthesize appDelegate = _appDelegate;

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).


Please do as follow.

//Your Header File 
#import "YourAppDelegate.h"

@interface YourViewController : UIViewController
{
    YourAppDelegate *appDelegate;
}

@property(nonatomic,retain) YourAppDelegate *appDelegate;

@end

//Your Implementation file
@implementation YourViewController
@synthesize appDelegate

- (void)viewDidLoad {
    self.appDelegate = (YourAppDelegate*) [[UIApplication sharedApplication] delegate];
}

@end

After declaring as above you will have access of appDelegate anywhere in your view controller.

Hope it helps.

0

精彩评论

暂无评论...
验证码 换一张
取 消