在UIViewController上加载数据:viewDidLoad和viewWillAppear - 什么是正确的逻辑?

时间:2011-06-28 15:07:12

标签: iphone ios viewdidload viewwillappear

我的第三个标签中有一个带有UIScrollViewController的tabBarControllerView。我在viewDidLoad和viewWillAppear中加载基本相同的东西,核心数据中的图片。我认为viewWillAppear是我可以用来更新UIScrollView中的视图的。唯一的事情是当我向核心数据添加图片时,没有显示正确的图片。

我想知道这是我的viewWillAppear逻辑还是我的上下文关闭?我正在努力修复我的上下文,所以如果我的逻辑是合理的(减去ManagedObjectContext)那么这将是很好听的 - 因为我可以回到处理上下文问题。

如果这是逻辑,那么如果你能指出我的缺点,我真的很感激: - )

标头文件

#import <UIKit/UIKit.h>
#import "PhotoViewController.h"

@interface MyTabBarViewController  : UIViewController<UIScrollViewDelegate>
{

    IBOutlet UIPageControl *pageControl;
    IBOutlet UIScrollView *scroller;
    UIImage *image;
    CGFloat scrollWidth;
    int pageNumber;
}

@property (nonatomic,retain)IBOutlet UIPageControl *pageControl;
@property (nonatomic,retain)IBOutlet UIScrollView *scroller;

-(IBAction)clickPageControl:(id)sender;

@end

实施文件......相关摘要

- (void)viewDidLoad
{
    [super viewDidLoad];
    scroller.delegate=self;
    scroller.pagingEnabled=YES;
    scroller.directionalLockEnabled=YES;
    scroller.showsHorizontalScrollIndicator=NO;
    scroller.showsVerticalScrollIndicator=NO;
    scroller.contentSize=CGSizeMake(pageControl.numberOfPages*scroller.frame.size.width, scroller.frame.size.height);
    scrollWidth = 0;
    pageNumber = 0;
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    NSManagedObjectContext *context = [(CoreDataProjAppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
    request.entity = [NSEntityDescription entityForName:@"Photo" inManagedObjectContext:context];
    NSError *error = nil;
    NSArray *fetchCount = [context executeFetchRequest:request error:&error];
    int pageCount = [fetchCount count];
    for (int i=pageNumber; i<pageCount; i++)
    {
        PhotoViewController *pvc = [[PhotoViewController alloc] initWithNibName:@"PhotoViewController" bundle:nil];
        CGRect rect = scroller.frame;
        rect.size.height = scroller.frame.size.height;
        rect.size.width = scroller.frame.size.width;
        rect.origin.x = scroller.frame.origin.x + scrollWidth;
        rect.origin.y = scroller.frame.origin.y;
        pvc.view.frame  = rect;
        [pvc view];
        pvc.label.text = [NSString stringWithFormat:@"%d", pageNumber];
        pvc.label.textColor = [UIColor redColor];
        Photo *photo = [[Photo alloc] init];
        photo = [fetchCount objectAtIndex:i];
        UIImage *fetchedImage = [UIImage imageWithData:photo.photo];
        pvc.imageView.image = fetchedImage;
        [scroller addSubview:pvc.view];
        [pvc release];
        pageNumber++;
        scrollWidth += scroller.frame.size.width;
    }
    pageControl.numberOfPages=pageCount;
    pageControl.currentPage=0;
    [self.view addSubview:scroller];
    [request release];
}

- (void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    scroller.delegate=self;
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    NSManagedObjectContext *context = [(CoreDataProjAppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
    request.entity = [NSEntityDescription entityForName:@"Photo" inManagedObjectContext:context];
    NSError *error = nil;
    NSArray *fetchCount = [context executeFetchRequest:request error:&error];
    int pageCount = [fetchCount count];
    scroller.contentSize=CGSizeMake(pageCount*scroller.frame.size.width, scroller.frame.size.height);
    for (int i=pageNumber; i<pageCount; i++)
    {
        PhotoViewController *pvc = [[PhotoViewController alloc] initWithNibName:@"PhotoViewController" bundle:nil];
        CGRect rect = scroller.frame;
        rect.size.height = scroller.frame.size.height;
        rect.size.width = scroller.frame.size.width;
        rect.origin.x = scroller.frame.origin.x + scrollWidth;
        rect.origin.y = scroller.frame.origin.y;
        pvc.view.frame  = rect;
        [pvc view];
        pvc.label.text = [NSString stringWithFormat:@"%d", pageNumber];
        pvc.label.textColor = [UIColor redColor];
        Photo *photo = [[Photo alloc] init];
        photo = [fetchCount objectAtIndex:i];
        UIImage *fetchedImage = [UIImage imageWithData:photo.photo];
        pvc.imageView.image = fetchedImage;
        [scroller addSubview:pvc.view];
        [pvc release];
        pageNumber++;
        scrollWidth += scroller.frame.size.width;
    }
    pageControl.numberOfPages=pageCount;
    pageControl.currentPage=0;
    [self.view addSubview:scroller];
    [request release];
}

1 个答案:

答案 0 :(得分:1)

我不知道这是否是实际原因,但有一点我注意到,在你的for循环中,你手动递增pageNumber++并且我不完全确定原因。如果您在循环中使用pageNumber的任何内部引用(我只看到您在其中设置标签文本的行)并将其替换为i,我相信这可以让您显示图像正常。简而言之,只需从代码中取出pageNumber(除非我明显忽略了某些内容),然后使用for (int i = 0; i < pageCount; i++) {。如果没有更多,这将使您更好地控制循环并消除每个循环增量期间的任何变化。至于使用哪种方法,最有可能最好在viewWillAppear中实现代码,因为它会为视图提供更多的加载机会,并在显示图像时为您提供适当的尺寸。

编辑:如果您希望按特定顺序显示图像,则无法通过从managedObjectContext中提取对象来直接执行此操作,因为它将对象存储为NSSet(未编制索引)。如果要按特定顺序显示图片,则需要使用排序描述符。因此,当您创建请求时,您需要遵循以下内容:

NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSManagedObjectContext *context = [(CoreDataProjAppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
request.entity = [NSEntityDescription entityForName:@"Photo" inManagedObjectContext:context];
NSError *error = nil;
NSSortDescriptor *sorter = [NSSortDescriptor sortDescriptorWithKey:@"someAttributeToIndex" ascending:YES];
[request setSortDescriptors:[NSArray arrayWithObject:sorter]];
NSArray *fetchCount = [context executeFetchRequest:request error:&error];

执行此操作应该让您实际指定要呈现图像的顺序,可以是添加日期,也可以是整数索引(您必须将其添加为实体的属性)