Go Back   MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Jun 11, 2010, 04:06 PM   #1
printf
macrumors regular
 
Join Date: Aug 2008
Create UIScrollView Programmatically

i'm going off this programming guide on how to create a scrollview programmatically, but i can't get it to work as there's a few things i don't understand http://developer.apple.com/mac/libra.../Creating.html

right now, i have a window, a custom uiview and a viewcontroller. i try to alloc the uiscrollview inside the viewcontroller's loadView method but it crashes with an unrecognized selector exception.

the problem here is i don't really understand the relationship between the scrollview and the view controller. i know i'll add my custom uiview as a subview to the scrollview, but beyond that not much.
printf is offline   0 Reply With Quote
Old Jun 11, 2010, 06:05 PM   #2
dejo
Moderator
 
dejo's Avatar
 
Join Date: Sep 2004
Location: The Centennial State
Let's see your code.
__________________
dejo is offline   0 Reply With Quote
Old Jun 11, 2010, 07:24 PM   #3
printf
Thread Starter
macrumors regular
 
Join Date: Aug 2008
ok.

//app delegate's didFinishLaunchingWithOptions:
Code:
viewController = [[ViewController alloc] initWithFrame:CGRectMake(0.0, 0.0, 320, 480)];
	
[window addSubview:viewController.view];
[window makeKeyAndVisible];
//relevant methods from view controller
Code:
-(id)initWithFrame:(CGRect)rect {
	NSLog(@"initWithFrame");
	if (self = [super init]) {		
		scrollView = [[UIScrollView alloc] initWithFrame:rect];
	}
	return self;
}

- (void)loadView {
	NSLog(@"loadView");
	
	myView = [[CustomView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320, 480)];
	myView.autoresizesSubviews = YES;
	self.view = myView;        

	[scrollView setContentSize: CGSizeMake(320, 960)];	// twice as long as the screen
	[scrollView setFrame:CGRectMake(0, 0, 320, 480)];
	
}

- (void)viewWillAppear:(BOOL)animated {
	[super viewWillAppear:animated];	
	
	scrollView.contentSize = CGSizeMake(self.view.frame.size.width,myView.frame.size.height);
	[scrollView addSubview:myView];	
}
printf is offline   0 Reply With Quote
Old Jun 13, 2010, 03:53 AM   #4
printf
Thread Starter
macrumors regular
 
Join Date: Aug 2008
ok, i got it working - finally.

basically it was a small oversight on my part. by changing this:

Code:
self.view = myView
to this:

Code:
self.view = scrollView;
it now works.
printf is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Resolved: How to create a UIImage RGB gradient with a portion of black programmatically troop231 iPhone/iPad Programming 5 Jan 13, 2014 10:25 PM
Add Item to Dock Programmatically BobRon Mac Programming 1 Sep 22, 2013 05:08 AM
Programmatically sending messages Mvkoe iPhone/iPad Programming 1 Jan 18, 2013 07:13 AM
Is there a way to programmatically change the language being used? NukemHill iPhone/iPad Programming 0 Dec 12, 2012 04:22 PM
How to create a Modal View Programmatically (and dismiss it) roeik iPhone/iPad Programming 10 Aug 15, 2012 03:36 PM

Forum Jump

All times are GMT -5. The time now is 12:13 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC