PDA

View Full Version : Problem with UIView, UIViewController and IB




Nayefc
Mar 31, 2011, 10:00 PM
Hello Everyone,

Okay so I am trying to create a grid of 6 UIImageViews which scrolls vertically (and can show up to 15). The way I am doing it is reusing the same 6 UIImageViews. Before fixing the actual scrolling, I want to be able to at least see the images on the view - they are not showing up.

So here's what I did. I subclass-ed UIView as follows (its still called ScrollingViewController becuase it was a ViewController before I changed it to become a UIView. I should rename it to ScrollingView)


@implementation ScrollingViewController

@synthesize scrollView, imageSet, imageView1, imageView2, imageView3, imageView4, imageView5, imageView6, image1Index, image2Index, image3Index, image4Index, image5Index, image6Index;

- (void)setImages:(NSArray *)images {

imageView1.frame = CGRectMake(683, 0, 341, 352);
imageView2.frame = CGRectMake(342, 0, 341, 352);
imageView3.frame = CGRectMake(0, 0, 341, 352);
imageView4.frame = CGRectMake(683, 352, 341, 352);
imageView5.frame = CGRectMake(342, 352, 341, 352);
imageView6.frame = CGRectMake(0, 352, 341, 352);

imageView1 = [imageSet objectAtIndex:0];
imageView2 = [imageSet objectAtIndex:1];
imageView3 = [imageSet objectAtIndex:2];
imageView4 = [imageSet objectAtIndex:3];
imageView5 = [imageSet objectAtIndex:4];
imageView6 = [imageSet objectAtIndex:5];

scrollView.contentSize = CGSizeMake([imageSet count] * 341, 352);
}

- (id) init {
self = [super init];
if(self != nil) {
scrollView = [[UIScrollView alloc] init];
scrollView.scrollEnabled = YES;
scrollView.pagingEnabled = NO;
scrollView.directionalLockEnabled = YES;
scrollView.showsVerticalScrollIndicator = YES;
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.delegate = self;
//scrollView.backgroundColor = [UIColor blueColor];
scrollView.autoresizesSubviews = YES;
scrollView.frame = CGRectMake(0, 0, 1024, 704);
//[self.view addSubview:scrollView];

imageView1 = [[UIImageView alloc] init];
[scrollView addSubview:imageView1];

imageView2 = [[UIImageView alloc] init];
[scrollView addSubview:imageView2];

imageView3 = [[UIImageView alloc] init];
[scrollView addSubview:imageView3];

imageView4 = [[UIImageView alloc] init];
[scrollView addSubview:imageView4];

imageView5 = [[UIImageView alloc] init];
[scrollView addSubview:imageView5];

imageView6 = [[UIImageView alloc] init];
[scrollView addSubview:imageView6];
}

return self;
}

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

self.scrollView.contentOffset = CGPointMake(1024, thisScrollView.contentOffset.y);

/**** Fix Scrolling here after it actually appears on the View
CGFLoat pageHeight = 1024;
float currPos = scrollView.contentOffset.y;
int selectedPage = roundf(currPos / pageHeight);
float truePosition = selectedPage * pageHeight;

if (currPos > 1024) {

}


///////////////////////////////////


CGFloat pageWidth = 1024;
float currPos = scrollView.contentOffset.x;

int selectedPage = roundf(currPos / pageWidth);

float truePosition = selectedPage * pageWidth;

int zone = selectedPage % 2;

BOOL view1Active = zone == 0;

UIImageView *nextView = view1Active ? view2 : view1;

int nextpage = truePosition > currPos ? selectedPage-1 : selectedPage+1;

if(nextpage >= 0 && nextpage < [imageSet count]) {
if((view1Active && nextpage == view1Index) || (!view1Active && nextpage == view2Index)) return;

NSLog(@"Load next image!");

nextView.frame = CGRectMake(nextpage*1024, 0, 1024, 704);
nextView.image = [imageSet objectAtIndex:nextpage];

if(view1Active) view1Index = nextpage;
else view2Index = nextpage;
}*/
}

- (void)dealloc {
[scrollView release];
[imageSet release];
[imageView1 release];
[imageView2 release];
[imageView3 release];
[imageView4 release];
[imageView5 release];
[imageView6 release];
[super dealloc];
}

@end


And then I have ViewController as follows (which gets pushed on top of the UINavigationController stack from another ViewController:


@implementation TopNewsViewController

@synthesize scrollingViewController;

- (id)init {

self = [super init];

if(self != nil) {
/*scrollingViewController = [[ScrollingViewController alloc] init];
scrollingViewController.view.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
scrollingViewController.view.autoresizesSubviews = YES;
[self.view addSubview:scrollingViewController.view];*/
[self.view addSubview:scrollingViewController];
}

return self;
}

- (void) viewDidLoad {
// Load an array of images into the page view controller
[scrollingViewController setImages:[NSArray arrayWithObjects:
[UIImage imageNamed:@"1.jpg"],
[UIImage imageNamed:@"2.jpg"],
[UIImage imageNamed:@"3.jpg"],
[UIImage imageNamed:@"4.jpg"],
[UIImage imageNamed:@"5.jpg"],
[UIImage imageNamed:@"6.jpg"],
[UIImage imageNamed:@"7.jpg"],
[UIImage imageNamed:@"8.jpg"],
[UIImage imageNamed:@"9.jpg"],
[UIImage imageNamed:@"1.jpg"],
[UIImage imageNamed:@"2.jpg"],
[UIImage imageNamed:@"3.jpg"],
[UIImage imageNamed:@"4.jpg"],
[UIImage imageNamed:@"5.jpg"],
[UIImage imageNamed:@"6.jpg"],
nil]];
}

#pragma mark - View Lifecycle / Memory Management
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}

- (void)viewDidUnload {
[super viewDidUnload];
self.scrollingViewController = nil;
}

- (void)dealloc {
[scrollingViewController release];
[super dealloc];
}

@end



How do I make this UIView appear in IB? In IB for TopViewController.xib, I created a View and connected it to the File's Owner and then created another View in it, and changed it to be a ScrollingViewController and connected it to the ScrollingViewController outlet in IB.
Here's how my IB is structured:
> View (UIView)
- ScrollingViewController (UIView)


Now when I run the app, the view that shows up is still white. When I add a Label in IB, it shows up when I run the app which means that my UIView drawings aren't showing up. Why is that? Can someone help me?

Any help is much appreciated.

Thank you,