为什么在NSObject协议中没有performSelectorOnMainThread :?
|
最近,我正在开发应用程序。 iPhone(iOS)项目。
我想知道为什么NSObject Protocol中没有performSelectorOnMainThread:。
我需要在主线程上调用委托的方法,因为它们必须处理UI组件。
这是我写的示例:
@protocol MyOperationDelegate;
@interface MyOperation : NSOperation {
id <MyOperationDelegate> delegate;
}
@property (nonatomic, assign) id <MyOperationDelegate> delegate;
@end
@protocol MyOperationDelegate <NSObject>
@optional
- (void) didFinishHandleWithResult:(NSDictionary *)result;
@end
@implementation MyOperation
@synthesize delegate;
- (void) main {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSDictionary *aDict = [[MySingleton sharedObject] fetchSomethingMeaningful];
//do something and call delegate
if ([delegate respondsToSelector:@selector(didFinishHandleWithResult:)]) {
[delegate performSelector:@selector(didFinishHandleWithResult:) withObject:
}
[pool release];
}
@end
@interface MyViewCon : UIViewController <MyOperationDelegate> {
NSOperationQueue *queue;
}
@end
@implementation MyViewCon
- (void) viewDidLoad {
MyOperation *op = [[MyOperation alloc] init];
op.delegate = self;
[queue addOperation:op];
[op release];
}
- (void) reloadUserInterface:(NSDictionary *)dict {
// Do reload data on User Interfaces.
}
- (void) didFinishHandleWithResult:(NSDictionary *)myDict {
// Couldn\'t execute method that\'s handling UI, maybe could but very slow...
// So it must run on main thread.
[self performSelectorOnMainThread:@selector(reloadUserInterface:) withObject:myDict];
}
@end
我可以在MyOperation类的主线程上运行didFinishHandleWithResult:委托方法吗?
从这里开始,每次调用MyOperation实例时,我就实现UI处理方法。
任何建议对我都会有帮助。
没有找到相关结果
已邀请:
3 个回复
稍惮
豪抱怒掳
确实具有方法
。您可以使用它。
蔬谢窗港