Iphone 重新加载RootView控制器或重新启动应用程序

Iphone 重新加载RootView控制器或重新启动应用程序,iphone,ios,Iphone,Ios,我不熟悉IOS编程,但我有一个问题,我已经坚持了一天多,我找不到解决方案。我有一个应用程序,安装后第一次运行时需要下载一些内容。下载完成后,作为RootView控制器的视图不会刷新该视图的数据。我可以导航到正确填充的其他视图,但此视图不正确。如果我关闭应用程序并重新打开它,数据就在那里。因此,我需要一种方法来强制视图完全重新加载或重新启动应用程序 #import <UIKit/UIKit.h> #import <CoreData/CoreData.h> @interf

我不熟悉IOS编程,但我有一个问题,我已经坚持了一天多,我找不到解决方案。我有一个应用程序,安装后第一次运行时需要下载一些内容。下载完成后,作为RootView控制器的视图不会刷新该视图的数据。我可以导航到正确填充的其他视图,但此视图不正确。如果我关闭应用程序并重新打开它,数据就在那里。因此,我需要一种方法来强制视图完全重新加载或重新启动应用程序

#import <UIKit/UIKit.h>

#import <CoreData/CoreData.h>

@interface RootViewController : UIViewController <NSFetchedResultsControllerDelegate>{

}
@property (nonatomic, retain) IBOutlet UIImageView *bannerImageView;
@property (nonatomic, retain) NSFetchedResultsController *fetchedResultsController;
@property (nonatomic, retain) NSManagedObjectContext *managedObjectContext;
-(void)populateBannerImages;
-(void)getimage;
如果您有任何建设性的想法或帮助,我们将不胜感激
  • 创建重新加载机制:通过NSNotificationCenter发送NSNotification,所有视图控制器都注册该通知。(然后,您可以重新执行fetchedResultsController的fetch请求,并调用
    [self.tableView重载数据]
  • 重新加载根视图控制器:让你的应用程序代理下载文件,然后创建一个新的根视图控制器,并调用
    [self.window setRootViewController:newController]
    这将重置整个视图(=>糟糕的用户体验)

  • 假设您的核心数据保存和获取正确完成,最好的方法是通过NSNotificationCenter。在AppDelegate类中,您需要在下载数据后将通知发布到通知中心。只有在数据下载完成后,才能发布此信息。也许是这样的:

    [[NSNotificationCenter defaultCenter] postNotificationName:@"DataFinishedDownloading" 
                                                        object:self];
    
    然后在RootViewController类中,首先需要注册RootViewController类以侦听该通知。可能在init方法中:

    -(id)init
    {
        self = [super init];
        if(self)
        {
             // We add this instance of RootViewController class as an observer of the "DataFinishedDowloading"
             // Once this notification is posted to the notification center from appDelegate
             // RootViewController will be notified and the method inside selector will be called
             [[NSNotificationCenter defaultCenter] addObserver:self 
                                                      selector:@selector(receivedDataFinishedDownloadingNotification:)
                                                          name:@"DataFinishedDownloading"
                                                        object:nil];
        }
    
        return self;
    }
    
    - (void)receivedDataFinishedDownloadingNotification:(NSNotification*)notification 
    {
        // Once the images are done downloading, you just need to refresh the tableView.  It will
        // then display the newly acquired data in your table cells.
        [tableView reloadData];
    }
    

    唯一可能出现的另一个问题(也可能不会)是,如果数据下载速度足够快。偶尔,如果数据下降速度足够快,并且viewController的ViewDidDisplay尚未出现,则通知方法将尝试在ViewDidDisplay出现之前重新加载数据。这可能会导致意外事故。通常,在调用重载数据之前,在ViewDid中显示并检查布尔集是一个好主意。希望这能有所帮助。

    使用
    NSNotificationCenter
    可能有些过分。既然你有一个<代码> NSFetchedResultsController <代码>,我会考虑设置你的视图控制器作为它的委托,并实现<代码> NStFraceReDigultMealthRealPraveSux.PrimeDealExchange:方法> < /P>
    对于您的情况,该方法的最简单实现将只调用
    [tableView reloadData]
    。有关更多信息,请参阅。

    您似乎正在使用核心数据进行存储。Core Data是一个功能强大的框架,但你需要是一名高级iOS开发人员才能使用它(而且你甚至不知道如何实现重新加载通知)。是的,Martin,我很欣赏评论中的傲慢和愤世嫉俗,但我已经在使用推送通知了,它们不起作用。核心数据不仅仅适用于IOS,因此其使用的先决条件不是成为高级IOS开发人员。我在ControllerDidChangeContent中缺少重新加载的数据。在我补充说我很好之后,我仍然感谢你迷人的、乐于助人的态度+1回答了真正的问题。操作-始终假设有一种方法可以在不重新启动应用程序的情况下完成所需的操作。
    [[NSNotificationCenter defaultCenter] postNotificationName:@"DataFinishedDownloading" 
                                                        object:self];
    
    -(id)init
    {
        self = [super init];
        if(self)
        {
             // We add this instance of RootViewController class as an observer of the "DataFinishedDowloading"
             // Once this notification is posted to the notification center from appDelegate
             // RootViewController will be notified and the method inside selector will be called
             [[NSNotificationCenter defaultCenter] addObserver:self 
                                                      selector:@selector(receivedDataFinishedDownloadingNotification:)
                                                          name:@"DataFinishedDownloading"
                                                        object:nil];
        }
    
        return self;
    }
    
    - (void)receivedDataFinishedDownloadingNotification:(NSNotification*)notification 
    {
        // Once the images are done downloading, you just need to refresh the tableView.  It will
        // then display the newly acquired data in your table cells.
        [tableView reloadData];
    }