这是你做什么:
indexSelected = [NSString stringWithFormat: @"%d",[indexPath row] ];
SubscribersDetailViews2 *viewController = [[SubscribersDetailViews2 alloc] initWithNibName:@"SubscribersDetailViews2" bundle:nil];
[[MySingletonClass sharedMySingleton] doAnAuthenticatedAPIFetch_Subscriber_Detail:indexSelected delegate:self];
[[self navigationController] pushViewController:viewController animated:YES];
[viewController release];
您定义您的视图控制器符合当获取和数据的分析做你调用委托的方法让一个协议视图控制器知道数据已准备好显示。
如果您需要更多关于如何操作的信息,请发表评论。
编辑:所以这里是如何声明和使用协议。我会尽量保持它尽可能简单。我不确定我是否喜欢你的命名约定,但我仍然会在这个例子中使用它。
所以让我们来看看代码。这是你如何声明一个协议:
@protocol MySingletonClassDelegate <NSObject>
@optional
- (void)didDoAnAuthenticatedAPIFetch_Subscriber_Detail_WithData:(NSArray *)data;
- (void)failedToDoAnAuthenticatedAPIFetch_Subscriber_Detail_WithError:(NSError *)error;
@end
再次,我不喜欢的命名约定。在Objective-C方法名称中不应该有下划线。
在MySingletonClass
的声明之前,协议应该在MySingletonClass.h
中定义。
我在协议中声明了两个方法,一个用于传递数据,另一个用于在失败时传递错误,以便通知用户它失败了。
要使用你需要以下协议:
@interface SubscribersDetailViews2 : UITableViewController <MySingletonClassDelegate>
您还需要实现在协议中声明的方法,但我会离开,落实到你。
由于数据的提取已经出现在后台,所以我不认为我需要解释如何做到这一点。需要记住的一件重要事情是您想要在主线程上执行委托方法。下面的代码做到这一点:
- (void)doAnAuthenticatedAPIFetch_Subscriber_Detail:(NSUInteger)index delegate:id<MySingletonClassDelegate>delegate {
// Fetching data in background
if (successful) {
[self performSelectorOnMainThread:@selector(didDoAnAuthenticatedAPIFetch_Subscriber_Detail_WithData:) withObject:data waitUntilDone:NO];
} else {
[self performSelectorOnMainThread:@selector(failedToDoAnAuthenticatedAPIFetch_Subscriber_Detail_WithError:) withObject:error waitUntilDone:NO];
}
}
的// Fetching data in background
应该由你的代码来代替只要是明确的。我假设你的代码产生了我使用的变量(NSArray * data,NSError * error,BOOL成功)。
就是这样,如果你需要澄清任何事情让我知道。
嗨,埃里克感谢您的早期答复可以plz指导我如何申报和使用协议。 – 2011-03-16 10:03:42
我当然可以。我会尽快找到时间。希望那会很快。 – 2011-03-16 10:10:11
您是否在'MySingletonClass'中将'doAnAuthenticatedAPIFetch_Subscriber_Detail:'更改为'doAnAuthenticatedAPIFetch_Subscriber_Detail:delegate:'? – 2011-03-17 10:02:13