如何从另一个类调用函数并从当前类重新加载该视图?

时间:2012-05-28 07:10:18

标签: iphone ios ios5 xcode4.3

我正在开发一个使用凹凸技术的应用程序。我有四个选项卡,其中一个是表格视图。我在app委托类中编写了这个凹凸API,这样当应用程序打开时它应该能够传输data.Transfer功能正常工作。但问题是我将数据插入sq-lite并且sqlite中的数据显示在其中一个标签栏项目视图中。所以当用户选择此标签栏项并接收时数据我想插入并重新加载视图与新的更改。在插入之前告诉我工作。但问题是重新加载视图。任何人都可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

您可以使用NSOperation在后台执行插入,并在插入/编辑记录时发布通知。将侦听器添加到要显示数据的View控制器。

因此,只要控制器收到通知,它就会调用该方法从数据库重新加载数据。

@implementation MyClass

- (void) dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [super dealloc];
}

- (id) init
{
    self = [super init];
    if (!self) return nil;


    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadData:) name:@"COREDATA_OBJECT_EDITED" object:nil];

    return self;
}

- (void) reloadData:(NSNotification *) notification
{
    if ([[notification name] isEqualToString:@"COREDATA_OBJECT_EDITED"])
        {
            //load records from database and reload tableview
        }
}

@end





//Method where you are saving data objects in some other class

- (void) saveDataObject
{

    //Save Data object, if saved successfully then post notification to listener to reload the data
    // All instances of MyClass will be notified
    [[NSNotificationCenter defaultCenter] postNotificationName:@"COREDATA_OBJECT_EDITED" object:self];

}