gpt4 book ai didi

iphone - 从后台操作调用导航 Controller 方法

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:57:22 25 4
gpt4 key购买 nike

我正在使用 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 调用,将其放入内存然后尝试调用主导航 Controller :

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

我希望在其上执行选择器的主导航 Controller 如下所示:

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。任何意见表示赞赏。

最佳答案

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

[target method:alerts]

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

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

关于iphone - 从后台操作调用导航 Controller 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5254929/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com