1

Title pretty much says it all, I am new to Xcode and am trying to make a button that changed the view's background image. I will have three button and when you click one they will change the background that is associated with the button. How would I accomplish this? I have looked around but have not found anything relevant or anything that worked.. Thanks in advance!

EDIT::

As for what I have, I have a project set up with a window and one view with a background image called "default@2x-568h@2x.png" I would like to be able to press a button and have it switch to "second.png". I have a button and I control dragged into my .h which made this:

@interface TeslameterViewController : UIViewController <CLLocationManagerDelegate> {

IBOutlet UIButton *button1; 
}
- (IBAction)button1:(id)sender;
@end

thats where I am lost.

EDIT AGAIN:

Found the fix via this video:

http://www.youtube.com/watch?feature=player_embedded&v=iKuGiJMgMiQ

UdayM
  • 1,725
  • 16
  • 34
Kabone
  • 85
  • 5
  • 13

4 Answers4

2

On the button Action event add this line.

 (void)Action:(UIButton *)sender
 {
     UIGraphicsBeginImageContext(self.view.frame.size);
     [[UIImage imageNamed:@"bg.png"] drawInRect:self.view.bounds];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    self.view.backgroundColor = [UIColor colorWithPatternImage:image];
 }
Rox
  • 909
  • 11
  • 31
0

try this:

self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"imageName.png"]];

and also see UIView Image Background for example project with complete code...

DharaParekh
  • 1,730
  • 1
  • 10
  • 17
0

You need to be more specific on what you need.

Within your xib file, create a UIImageView and stretch its dimensions to fit the size of the screen. Also create a the necessary buttons. Create properties in your view controller to have a reference to the UIImageView and the buttons. Give the buttons an action that changes the picture that the UIImageView loads.

Byron S
  • 99
  • 2
  • 9
0

You have to create three buttons in nib file and add outlet to your view controller. To create outlet you have right click on button then you will get list of event, then control drag to your view controller header file, then one small window will pop up, write method name and click enter. Now u will see method in your controller.write code to change background of button or your view in that method. for more details:http://developer.apple.com/library/ios/#documentation/iphone/conceptual/iphone101/Articles/05_ConfiguringView.html

Rajath Shetty K
  • 424
  • 3
  • 9