gpt4 book ai didi

iphone - 重新加载 RootView Controller 或重启 App

转载 作者:行者123 更新时间:2023-11-28 23:12:28 26 4
gpt4 key购买 nike

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

#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;

这是我的 .m 文件

@implementation RootViewController
@end
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.title=@"MyTitle";

}
- (void)viewWillAppear:(BOOL)animated
{

[super viewWillAppear:animated];

}

- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [[self.fetchedResultsController sections] count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section
{
id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
return [sectionInfo numberOfObjects];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
[self configureCell:cell atIndexPath:indexPath];
return cell;
}

我使用下面的代码示例在应用委托(delegate)中运行下载功能的对话框。我知道问题是我从 appDidFinishLAunching 调用了 UIAlert,此时 rootViewController 已经加载,但我不确定是否有更好的方法来执行此操作。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:    (NSDictionary *)launchOptions
{
BOOL hasRunBefore=[[NSUserDefaults standardUserDefaults] boolForKey:@"FirstRun"];
if (hasRunBefore) {
}else if(!hasRunBefore){
UIAlertView *firstRunAlert =[[UIAlertView alloc]initWithTitle:@"Welcome" message:@"" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"DownLoad", nil];
[firstRunAlert show];
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"FirstRun"];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
NSString *buttontitle = [alertView buttonTitleAtIndex:buttonIndex];
if ([buttontitle isEqualToString:@"DownLoad"]) {
/download stuff here
}
}

任何建设性的想法或帮助将不胜感激

最佳答案

假设您的核心数据保存和获取正确完成,最好的方法是通过 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 的 viewDidAppear 还没有发生,通知方法将尝试在 viewDidAppear 发生之前重新加载数据。这可能会导致崩溃,这在极少数情况下会发生。通常,在调用 reloadData 之前在 viewDidAppear 中设置 bool 值并进行检查是个好主意。希望这会有所帮助。

关于iphone - 重新加载 RootView Controller 或重启 App,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7837546/

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