2011-03-10 59 views
2

我正在運行使用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

這聽起來像是你在查找正確的格式化電話到performSelectorOnMainThread:withObject:waitUntilDone:時遇到了一些麻煩。這很簡單,真的。 如果你通常會調用此類方法:

[target method:alerts] 

格式化performSelector調用是這樣的:

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

是的,我明白那部分,我實際上傳遞目標和選擇到GetRSSOperation 。我的問題是它沒有執行它,所以我假設我的目標是錯的... – 2011-03-10 03:14:23

+0

然後發佈更多的代碼。將您現有的代碼減少到一個測試用例,它只能說明問題,但實際上確實說明了問題。 – Anomie 2011-03-10 03:18:09

+0

上面的代碼已經發布到原來的問題上。 – 2011-03-10 20:52:55

相關問題