Assume in an app you have 2 UIButton
's, buttonA and buttonB. If you want to call the FlipsideViewController
from these 2 buttons, where the only the difference will be the background images. (i.e.: if buttonA is pressed, BackGroundA will appear in the 开发者_如何学JAVAFlipsideViewController
's view, otherwise, it will be BackGroundB.)
Now the First BackGround (BackGroundA) is set by default. How do I handle the second background image (BackGroundB) if buttonB is pressed?
Depending on how you are presenting FlipsideViewController, a couple of ways are:
- Make "background" a property of FlipsideViewController and set it as needed in each button's action method before showing the vc.
- Add a custom init method in FlipsideViewController with a "background" parameter.
"background" could be an int or enum property/parameter and then the code in FlipsideViewController will do whatever it needs to itself based on that value.
Edit:
To use the property approach:
First, in FlipsideViewController, make sure you have an IBOutlet for the UIImageView called say backgroundImageView
.
Next, in FlipsideViewController.h, add a property to set the background (I'm using an int):
@interface FlipSideViewController : UIViewController {
int backgroundId;
}
@property (assign) int backgroundId;
Next, in FlipsideViewController.m, add this:
@synthesize backgroundId;
-(void)viewWillAppear:(BOOL)animated
{
if (backgroundId == 2)
self.backgroundImageView.image = [UIImage imageNamed:@"background2.png"];
else
self.backgroundImageView.image = [UIImage imageNamed:@"background1.png"];
}
Finally, in the main view controller, the button action method would look something like this:
-(IBAction)buttonPressed:(UIButton *)sender
{
FlipSideViewController *fsvc = [[FlipSideViewController alloc] initWithNibName:nil bundle:nil];
fsvc.backgroundId = sender.tag; //assuming btn1.tag=1 and bnt2.tag=2
[self presentModalViewController:fsvc animated:YES];
[fsvc release];
}
精彩评论