Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 03-31-2011, 05:37 PM   #1 (permalink)
Registered Member
 
Join Date: Mar 2011
Posts: 5
Nayefc is on a distinguished road
Lightbulb Problem with UIView, UIViewController and IB

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)

Code:
@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:

Code:
@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,
Nayefc is offline   Reply With Quote
Old 03-31-2011, 08:14 PM   #2 (permalink)
Registered Member
 
Join Date: Mar 2011
Posts: 5
Nayefc is on a distinguished road
Default

Any suggestions?
Nayefc is offline   Reply With Quote
Old 03-31-2011, 09:30 PM   #3 (permalink)
Super Moderator
 
Join Date: Jan 2011
Posts: 434
Rhade is on a distinguished road
Default

Wait a minimum of 12 hours before bumping a thread.

Thread Bumping Guidelines

Consider this a warning.
Rhade is offline   Reply With Quote
Reply

Bookmarks

Tags
cocoa-touch, ipad, uiscrollview, uiview, uiviewcontroller

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 375
12 members and 363 guests
condor304, dansparrow, dre, ilmman, LezB44, michelle, Objective Zero, samdanielblr, Sami Gh, shagor012, thephotographer, tinamm64
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,663
Threads: 94,119
Posts: 402,896
Top Poster: BrianSlick (7,990)
Welcome to our newest member, LezB44
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 01:37 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0