Question

I am trying to add an array of user interactive views to my existing code. I have the following code with an array that works using a scrollview (with page control) to display a set of images. I want to modify the code to display an array of user interactive views that have been created in interface builder. By having user interaction, these views will have buttons and zoomable image views.

-----Current Code-----

Implementation file(.m) of view controller with scrollview

#import "AboutViewController.h"

@interface AboutViewController ()

@end

@implementation AboutViewController

@synthesize scrollView;
@synthesize pageControl;
@synthesize imageArray;

int page;

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

- (void)viewDidLoad
{
    [super viewDidLoad];
    //Can we load and display ViewControllers as opposed to images?  
    imageArray = [[NSArray alloc] initWithObjects:@"1.png", @"2.png", @"3.png", nil];

    for (int i = 0; i < [imageArray count]; i++ ) {
       int page = scrollView.contentOffset.x / scrollView.frame.size.width;

       CGRect frame;
       frame.origin.x = self.scrollView.frame.size.width * i;
       frame.origin.y = 0;
       frame.size = self.scrollView.frame.size;

        UIImageView *imageView = [[UIImageView alloc] initWithFrame:frame];
        imageView.image = [UIImage imageNamed:[imageArray objectAtIndex:i]];
        [self.scrollView addSubview:imageView];

       NSLog (@"page %d",page);

    }

    scrollView.contentSize = CGSizeMake(scrollView.frame.size.
                             width *[imageArray count], 
                          scrollView.frame.size.height);
}

#pragma mark - UIScrollView Delegate
- (void)scrollViewDidScroll:(UIScrollView *)sender
{

    CGFloat pageWidth = self.scrollView.frame.size.width;
    //calculate current page in scrollview 
    int page = floor((self.scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
    self.pageControl.currentPage = page;
    NSLog (@"page %d",page);
}

- (IBAction)done:(id)sender
{
    [self.delegate aboutViewControllerDidFinish:self];

}

@end

Header file(.h) of the view controller with scrollview

#import <UIKit/UIKit.h>

@class AboutViewController;

@protocol AboutViewControllerDelegate

- (void)aboutViewControllerDidFinish:(AboutViewController *)controller;
@end

@interface AboutViewController : UIViewController<UIScrollViewDelegate>


@property (strong, nonatomic,retain) id <AboutViewControllerDelegate> delegate;
@property (nonatomic,strong,) IBOutlet UIScrollView *scrollView;
@property (nonatomic, strong) IBOutlet UIPageControl *pageControl;
@property (nonatomic, strong) NSArray *imageArray;
@property(nonatomic, assign) IBOutlet UILabel *label;

- (IBAction)done:(id)sender; //returns back to main menu after a button is pressed

@end

---------------------------Update----------------------------------

Further Clarification:

For those who may be joining this question, Earlier I was confused with embedding view controllers in the scrollview as opposed to views. I now believe it is not possible to embed a view controller in a scrollview but it is possible to embed views into it.

Was it helpful?

Solution 2

An NSArray (and any instance of its subclasses, such as NSMutableArray) can hold any kind of object. A view controller is an object, so an NSArray instance can hold it.

Notice that the array holds strong references to the objects it contains, so if your object is added to an array it will stay in memory even if all other references are removed (for instance by setting them to nil). Until you remove the object from the array, of course.

All of these considerations are valid for sets and dictionaries, too, btw.

OTHER TIPS

Maybe you are confused with UIView and UIViewController. Only UIView can be displayed. For example, your imageView is an instance of UIImageView(which is a subclass of UIView), so it can be presented on your screen. UIViewController is responsible for creating and configuring UIView instances and cannot be displayed.

Normally, a UIViewController is made to control a full-screen view which you can add subviews on. If you want to present several images on your screen, a UIScrollView with a UIPageControl will be sufficient.

If you need to build multiple pages and each page has its dependent appearance and user interaction, you could choose UINavigationController to manager multiple UIViewController instances and create and configure each page in its correlate UIViewController.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top