开发者

UIImage VIew Not loading image?

开发者 https://www.devze.com 2023-04-01 12:23 出处:网络
I have a UIImage on my MyCartViewController. And i have a button on my BlackFacePlatesViewController. I want this button to set the image on the MyCartViewController to another image, however i can\'t

I have a UIImage on my MyCartViewController. And i have a button on my BlackFacePlatesViewController. I want this button to set the image on the MyCartViewController to another image, however i can't seem to get it to work. Here is my code for the button to set the image on the MyCartViewController:

EDIT:

I HAVE 1 BOOL VARIABLE IN VC2 AND WHEN THE BUTTON IS PRESSED IN VC1, I SET THE BOOL AND CREATE VC2. THEN IN THE VIEWWILLAPPEAR METHOD, I SET THE IMAGE ACCORDINGLY. HERE IS THE CODE: THANKS

- (IBAction)outlet1 {
MyCartViewController * imageCart = [[MyCartViewController alloc]init];
imageCart.displayImage = YES;

}

BUTTON THAT THE USER PRESSES ^

THIS IS TO SWITCH PAGES:

- (IBAction)myCart:(id)sender; {
MyCartViewController * first = [[MyCartViewController alloc]init];
first.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:first animated:YES];
[first release];

}

THIS IS THE VIEWWILLAPPEAR METHOD IN VC2:

-(void)viewWillAppear:(BOOL)animated{
if (self.displayImage == YES) {
   开发者_如何学Go UIImage * myImage = [UIImage imageNamed:@"Paddle_3.png"];
    [outletImageView setImage:myImage];
}

}

WOULD THIS WORK? THANK YOU VERY MUCH FOR THE HELP EVERYBODY!!!


You aren't allocating your object cart properly, and cart2 has nothing to do with cart, apart from being a similar class. You should just make an instance variable in the header file that's a UIImage which holds the image you want to use in your next UIView, and set it when you use presentModalViewController: Try this. In your .h file,

@interface MyCartViewController : UIViewController {
  UIImage * imageForNextView;
}

In your .m file,

- (IBAction)outlet1:(id)sender; {
  imageForNextView = [UIImage imageNamed:@"Paddle_1.png"];
}

- (IBAction)outlet2:(id)sender; {
  imageForNextView = [UIImage imageNamed:@"Paddle_2.png"];
}

- (IBAction)myCart:(id)sender; {
  MyCartViewController * cart2 = [[MyCartViewController alloc]init];
  cart2.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
  cart2.newImage.image = imageForNextView;
  [self presentModalViewController:cart2 animated:YES];
  [cart2 release];
}

I made 2 button methods to illustrate the point of switching the image based on which button was pushed. Remember that imageNamed: caches the UIImage, so if you have a lot, you may have to start looking into other loading methods, all of which can be found here. This should solve your problem.

Edit: Try this. In your .h file,

@interface MyCartViewController : UIViewController {
  NSString * nameOfImage;
  UIImageView * imageView;
}

@property (nonatomic, retain) NSString * nameOfImage;

In the .m file,

@synthesize nameOfImage;

- (void)viewDidLoad; {
  [super viewDidLoad];
  imageView.image = [UIImage imageNamed:nameOfImage];
}

Then, when you push to the view, try:

- (IBAction)myCart:(id)sender; {
  MyCartViewController * cart2 = [[MyCartViewController alloc]init];
  cart2.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
  cart2.nameOfImage = @"Paddle_2.png";
  [self presentModalViewController:cart2 animated:YES];
  [cart2 release];
}

Edit2: This is a tutorial about how to use a UISplitViewController, which has a similar idea, where you press a UITableViewCell, and it changes an image in another UIView. You should look through this, and see if it can help your project.

Also, try:

- (IBAction)myCart:(id)sender; {
  MyCartViewController * cart2 = [[MyCartViewController alloc]init];
  cart2.nameOfImage = @"Paddle_2.png";
  [self pushViewController:cart2 animated:YES];
  [cart2 release];
}

This is another way to move to a new UIView, which may work.

Hope that Helps!


Are you sure this line of code is actually executed? Did you check that while debugging or with an NSLog? If not then check the reference to the action 'outlet1'.

What is cart? What is cart.newimage? How are those linked to the UIImagaeView that is part of 'the next page'?

You may need to force the redraw via setNeedsDisplay.

Assuming your cart.newImage is the UIImageView object:

[cart.newimage setNeedsDisplay]; //might do the trick. 

However, that is a bit view of code to really understand the issue.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号