从后台操作调用导航控制器方法

时间:2011-03-10 02:54:51

标签: iphone objective-c ios background nsoperation

我正在使用NSOperation运行后台操作,该操作从我的AppDelegate开始。我想要做的是当操作完成后,让它调用RootViewController.m文件中的方法。

目前我的AppDelegate中有一个名为navigationController的属性,我一直试图设置这样的调用:

AppDelegate.m:

GetRSSOperation *gro = [[GetRSSOperation alloc] initWithURL:rssURL target:self selector:@selector(dataSourceDidFinishLoadingNewData:)];
    [queue addOperation:gro];
    [gro release];

GetRSSOperation.m:

[target performSelectorOnMainThread:selector withObject:alerts waitUntilDone:YES];

我已尝试将目标设置为self,self.navigationController,self.navigationController.view但无效。

我知道这个问题可能是因为我还没有完全了解整个ios架构,所以请各位指点一下。

**添加更多代码来解释:

AppDelegate.h

#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>


@interface AppDelegate : NSObject {
    NSOperationQueue *queue;
    UIWindow *window;
    UINavigationController *navigationController;

    NSURL *rssURL;

    // storage for the alert list from RSS feed
    NSMutableArray *alerts;     
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UINavigationController *navigationController;
@property (nonatomic, retain) NSMutableArray *alerts;
@property (nonatomic, retain) NSURL *rssURL;
@property (retain) NSOperationQueue *queue;


+ (id)shared;
@end

AppDelegate.m

#import "AppDelegate.h"
#import "RootViewController.h"
#import "AlertViewController.h"
#import "RefreshTableHeader.h"
#import "GetRSSOperation.h"

@implementation AppDelegate
static AppDelegate *shared;

@synthesize window, navigationController, alerts;


- (id)init
{
    if (shared) {
        [self autorelease];
        return shared;
    }
    if (![super init]) return nil;

    queue = [[NSOperationQueue alloc] init];
    shared = self;
    return self;
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

        // Add the navigation controller's view to the window and display.
        UIImageView *imageview=[[UIImageView alloc]initWithFrame:CGRectMake(270, 3, 37, 37)];
        imageview.image=[UIImage imageNamed:@"iphone.png"];
        [self.navigationController.navigationBar addSubview:imageview]; 
        [self.window addSubview:navigationController.view];
        [self.window makeKeyAndVisible];

        if (rssURL == nil) {
            NSString *alertAddress = @"http://www.compoundstockearnings.com/com/public/selections.cfc?method=getrss&email=";
            alertAddress = [alertAddress stringByAppendingString:cseLogin];
            alertAddress = [alertAddress stringByAppendingString:@"&password="];
            alertAddress = [alertAddress stringByAppendingString:csePass];      
            rssURL = [[NSURL alloc] initWithString:alertAddress];
        }

        GetRSSOperation *gro = [[GetRSSOperation alloc] initWithURL:rssURL target:self selector:@selector(dataSourceDidFinishLoadingNewData:)];
        [queue addOperation:gro];
        [gro release];      
        return YES;
    }
    return NO;
}
@end

GetRSSOperation只是调用rss,将其放入内存然后尝试调用主导航控制器:

[target performSelectorOnMainThread:selector withObject:alerts waitUntilDone:YES];

我希望事物执行选择器的主导航控制器如下所示:

RootViewController.h

#import <UIKit/UIKit.h>

@interface RootViewController : UITableViewController {
    IBOutlet RefreshTableHeader *alertTable;

    UIImageView *imageView; 

}

@end

RootViewController.m:
#import "RootViewController.h"
#import "AppDelegate.h"

@implementation RootViewController

- (void)dataSourceDidFinishLoadingNewData:(NSMutableArray *)tempAlerts 
{
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    appDelegate.alerts = tempAlerts;
    reloading = NO;
    [alertTable flipImageAnimated:NO];
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:.3];
    [self.tableView setContentInset:UIEdgeInsetsMake(0.0f, 0.0f, 0.0f, 0.0f)];
    [alertTable setStatus:kPullToReloadStatus];
    [alertTable toggleActivityView:NO];
    [UIView commitAnimations];
    [popSound play];
}

显然我已经缩短了一点但这是它的核心。我只是希望GetRSSOperation能够调用RootViewController.m - dataSourceDidFinishLoadingNewData。任何投入赞赏。

1 个答案:

答案 0 :(得分:1)

听起来你在确定如何正确格式化performSelectorOnMainThread:withObject:waitUntilDone:的调用时遇到了一些麻烦。真的很简单。 如果你通常会这样调用这个方法:

[target method:alerts]

您可以像这样格式化performSelector调用:

[target performSelectorOnMainThread:@selector(method:) withObject:alerts waitUntilDone:YES];