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

Reply
 
Thread Tools Search this Thread Display Modes
Old Aug 31, 2008, 12:52 PM   #1
tsornin
macrumors newbie
 
Join Date: Jul 2002
Setting background image programmatically?

I'm trying to figure out how to set a background image programmatically. I've created a Utility application in xcode, so it has a main view and a flipside view, plus a root view controller. The main view consists of the view class (MainView) and the view controller (MainViewController). The view itself is created in Interface Builder.

Now, I've written a fairly simple method on MainView.m :

Code:
- (void)setBackgroundImage {
	UIImageView *customBackground = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"background.jpg"]];
	background = customBackground;
	[customBackground release];
	
	[self addSubview:background];
	NSLog(@"Added background subview %@", background);
	[self sendSubviewToBack:background];
}
However, I can't figure out how to call this. The initWithFrame method on MainView.m is never called (I guess because the view is created in Interface Builder). I've tried adding this line to the MainViewController's initWithNibName function:
Code:
[self.view setBackgroundImage];
but that just results in a crash, saying "objc[34787]: FREED(id): message superview sent to freed object=0x453910".

How can I call custom methods on MainView.m from the view controller object?

I'm guessing maybe I need to create an instance of MainView, but isn't that what self.view is supposed to be?

Thanks,
-j.
tsornin is offline   0 Reply With Quote
Old Sep 1, 2008, 04:30 AM   #2
Taum
macrumors member
 
Join Date: Jul 2008
Quote:
The initWithFrame method on MainView.m is never called (I guess because the view is created in Interface Builder).
That's correct. You can use the view's awakeFromNib method, or probably the controller's viewDidLoad method (and initWithNibName also sounds ok).


Quote:
that just results in a crash, saying "objc[34787]: FREED(id): message superview sent to freed object=0x453910"
You alloc an object and release it before using it. You should add it as a subview and *then* release it.
background = customBackground does not retain you object. Perhaps you meant self.background = customBackground, with background being a synthesized property flagged as retain ?
If you don't understand why your code dealloc the object when you call release on line 3, you should get more familiar with the retain-release model used through the iPhone SDK.
Taum is offline   0 Reply With Quote
Old Sep 1, 2008, 09:59 AM   #3
tsornin
Thread Starter
macrumors newbie
 
Join Date: Jul 2002
Ah, thank you! That does seem obvious now. I've changed the setBackgroundImage method to this:
Code:
- (void)setBackgroundImage {
	NSLog(@"setting bg image");
	UIImageView *customBackground = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"background.jpg"]];
	self.background = customBackground;
	[customBackground release];
	
	[self addSubview:background];
	NSLog(@"Added background subview %@", background);
	[self sendSubviewToBack:background];
}
Then added the call to it in the MainViewController's initWithNibName method:

Code:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
	if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
		// Custom initialization
		[(MainView *)self.view setBackgroundImage];
	}
	NSLog(@"initWithNibName - view=%@", self.view);
	return self;
}
Also noticed that while [self.view setBackgroundImage]; works, it raises a compiler warning in Xcode, "Messages without a matching method signature will be assumed to return 'id' and accept '...' as arguments." I ended up changing it to [(MainView *)self.view setBackgroundImage]. Not sure if that's really the right way to do it, but it does work.
tsornin is offline   1 Reply With Quote
Old Sep 1, 2008, 12:23 PM   #4
Taum
macrumors member
 
Join Date: Jul 2008
It is the right way to do it : view is a UIView for any UIViewController, but in your case you can guarrantee that it will always be a MainView. You can safely cast it into a MainView to call methods specific to MainView on it.
Taum is offline   0 Reply With Quote
Old Feb 17, 2009, 12:08 AM   #5
I'm a Mac
macrumors 6502
 
Join Date: Nov 2007
Now how would you change how the image displays without IB? For example, in interface builder, I would set the image to display as "bottom". How would I do this programmatically?
__________________
iMac 24-inch 2.8GHz Intel C2E | 4GB RAM | 500GB HDD
MacBook Unibody 2.4GHz Intel C2D| 2GB RAM | 250GB HDD
iPhone 3GS 16GB
I'm a Mac 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
Background image does not fit PlutoM4 iPhone/iPad Programming 2 Apr 19, 2014 09:53 AM
iPhone: SMS Background image GRay2102 Jailbreaks and iOS Hacks 13 Jan 14, 2014 05:02 AM
combine a background image, another image and a video in iMovie 10? MacMan988 Digital Video 0 Oct 27, 2013 04:52 AM
Cocoa Application: Setting Custom Background Image ajbudis Mac Programming 1 Jul 22, 2013 11:42 AM
Background Image rheb1026 iPhone/iPad Programming 2 Jul 29, 2012 10:24 PM

Forum Jump

All times are GMT -5. The time now is 02:46 PM.

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

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