2013-04-27 132 views
2

我正在構建一個應用程序,其中包含一些滾動內容。總之,這是我的配置:添加UIViewController作爲UIScrollView的子視圖

1)class_1:與內(類似於照相機行東西元素集合中的視圖)通過使用收集視圖中列出

2)class_2:模態的視圖稱爲由class_1在哪裏顯示每個單元素(從class_1)。同一類中,我也實現了所有元素的水平滾動

3)class_3:與ImageView的一個簡單的視圖控制器內將包含我會在class_2

顯示元件中運行我的應用程序看起來像class_3不作爲class_2的子視圖添加,並且我嘗試加載的所有單數內容都不可見。

下面是我class_2代碼

接口:

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

@interface class_2 : UIViewController 

@property (strong, nonatomic) IBOutlet UIPageControl *pageControl; 
@property (strong, nonatomic) IBOutlet UIScrollView *scrollView; 
@property (weak, nonatomic) NSArray *imageList; 

@end 

實現:

@interface class_2() { 
    NSMutableArray *_viewControllers; 
} 

    - (void)loadScrollViewWithImage:(NSUInteger)indexImage; 

@end 

@implementation class_2 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    // _imageList is passed from class_1 when I call class_2. Contains an array of images 
    self.pageControl.numberOfPages = [_imageList count]; 
    self.pageControl.currentPage = 0; 

    // I store inside an array N objects as the number f images I have to show. At the beginning the array has only null objects, then it will contains the views based on the images I have scrolled. 
    //I am storying my images inside a view because maybe I will show more controls and not just the image. It will be much easier manage them. 
    _viewControllers = [[NSMutableArray alloc] init]; 
    for (NSUInteger i = 0; i < [_imageList count]; i++) { 
     [_viewControllers addObject:[NSNull null]]; 
    } 

    // I set some properties of my scrollview defined as outlet 
    self.scrollView.frame = CGRectMake(0, 0, 320, 345); 
    self.scrollView.contentSize = CGSizeMake(CGRectGetWidth(self.scrollView.frame) * [_imageList count], CGRectGetHeight(self.scrollView.frame)); 
    self.scrollView.pagingEnabled = YES; 
    // this is set to NO, I switched to YES just now for debug (at least I can see how big the area is since there are no images visible) 
    self.scrollView.showsHorizontalScrollIndicator = YES; 
    self.scrollView.showsVerticalScrollIndicator = NO; 
    self.scrollView.scrollsToTop = NO; 
    self.scrollView.delegate = self; 

    // for testing now I always take the first and second elements of my array. I load both so when I move from the first image to the second is already there and there is no gap/flashing effect 
    [self loadScrollViewWithImage:0]; 
    [self loadScrollViewWithImage:1]; 
} 

#pragma mark - Private methods 

- (void)loadScrollViewWithImage:(NSUInteger)indexImage { 
    // I scrolled until the last image, from here I can only go back 
    if (indexImage >= [_imageList count]) 
     return; 

    // I replace the placeholder (null) object 
    class_3 *controller = [_viewControllers objectAtIndex:indexImage]; 
    if ((NSNull *)controller == [NSNull null]) { 
     controller = [[class_3 alloc] init]; 
     [_viewControllers replaceObjectAtIndex:indexImage withObject:controller]; 
    } 

    // I add the controller's view to the scroll view - in case this is the first time I'm scrolling this image 
    if (controller.view.superview == nil) { 
     // I set my controller frame as the scrollview works as container 
     CGRect frame = self.scrollView.frame; 
     frame.origin.x = CGRectGetWidth(frame) * indexImage; 
     frame.origin.y = 0; 
     controller.view.frame = frame; 

     // I finally add my viewcontroller as child of my superview and subview of the scrollview 
     [self addChildViewController:controller]; 
     [self.scrollView addSubview:controller.view]; 
     [controller didMoveToParentViewController:self]; 

     // I set an image corrisponding with the element selected from class_1 
     controller.imageView.image = [_imageList objectAtIndex:indexImage] 

     // I also tried with a fake image...just in case. No one of them is visible 
     //controller.imageView.image = [UIImage imageNamed:@"tempPhoto"]; 
    } 
} 

我class_3是它承載僅一個圖像視圖shoulb與selezted相應的圖像來填充一個的ViewController之前。 頁面控制器用於顯示我滾動的位置(letf/right)以及有多少個元素。滾動通過滾動視圖進行物理管理。 這裏是我的class_3定義

接口:

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

@interface class_3 : UIViewController 

@property (strong, nonatomic) IBOutlet UIImageView *imageView; 

@end 

實現(沒有讓人印象深刻)

#import "class_3.h" 

@interface class_3() 

@end 

@implementation class_3 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view. 

    self.imageView.image = [UIImage imageNamed:@"tempPhoto"]; 
} 

@end 

我缺少什麼?

TNX

回答

0

class_3應該是一個UIView,而不是UIViewController,子類。見here

+0

Tnx,我明白了。實際上你甚至可以使用UIViewController的子類,並通過使用initWithNibName:bundle:而不是initWithFrame來初始化你的類:(如果你創建了一個UIView子類的話)。我的代碼在兩種情況下都能正常工作(使用UIViewController和UIView的子類),但我做的錯誤是忘記從標識符實例化我的ViewController(因爲我使用的是故事板)。所以我解決了這一行[myStoryBoard_instance instantiateViewControllerWithIdentifier:@「」] :) – 2013-04-28 09:05:45

相關問題