How to change image of a button? - iphone

im getting a EXC_BAD_ACCESS error on this line.
[(CCMenuItemSprite*)[profileSelectionMenu getChildByTag:333] setNormalImage:normalSprite3];
Basically im just trying to change the normal image of a CCMenuItemSprite.
normalSprite3 and selectedSprte3 are both CCSprite. and i set the properties off them to retain but it still always crashing on the above line. Is there an easier way to do what im trying to accomplish? basically set the button to be a toggle button? and it changes image everytime it is pressed
normalSprite3 =[CCSprite spriteWithFile:#"main_menu_button.png"];
selectedSprite3 =[CCSprite spriteWithFile:#"main_menu_button_select.png"];
profile3MenuItem = [CCMenuItemImage itemFromNormalSprite:normalSprite3 selectedSprite:selectedSprite3
target:self
selector:#selector(P3:)];
[profile3MenuItem setTag:333];
[(CCMenuItemSprite*)[profileSelectionMenu getChildByTag:333] setNormalImage:normalSprite3];
Thanks for any help
G

Why not just
[profile3MenuItem setNormalImage:normalSprite3];
Also, where do you add the item to profileSelectionMenu so that getChildByTag works?
UPDATE: I wrote this to help with debugging EXC_BAD_ACCESS
http://loufranco.com/blog/files/Understanding-EXC_BAD_ACCESS.html
If you think you are releasing early, turn on NSZombiesEnabled
http://loufranco.com/blog/files/debugging-memory-iphone.html

Related

CGPointMake doesn't work in viewDidLoad

I have a button that I need to move on viewDidLoad but it doesn't want to move...
strange thing is that if I use the same exact code on a -(IBAction) then it moves as it should... any clue why is this happening?
the code I'm using to move the button is this:
self.myButton.center = CGPointMake(50, 120);
thanks for any help.
Peace,
Massy
For anyone that run in the same problem I found the solution in this tread: presentViewController: crash on iOS <6 (AutoLayout)
removing the "Use Autolayout" option let my app work fine.

iphone sdk how to switch an image and save it with a button press

I'm writing an application where I want an image to be displayed on the first page, but only on a small portion of the front page. What I've been trying to do is set up a button on a subview that will switch the image on the first view. This has had me stumped for hours.
Can anyone help?
Assuming that your view controller has a member variable named 'myImageView' that's hooked up to the actual UIImageView, then you can change a UIImageView's image, as follows (caveat: I haven't compiled this code so it may have minor errors):
// create some images
UIImage *imgPig = [[UIImage imageNamed:#"pig.png"] retain];
UIImage *imgCow = [[UIImage imageNamed:#"cow.png"] retain];
...
// sometime later, change the imageview to show a pig
[[self myImageView] image] = imgPig;
...
// sometime later, change the pig to a cow
[[self myImageView] image] = imgCow;
// finally release images
[imgPig release];
[imgCow release];
Or is the problem that you have about how to access the actual UIImageView? Or is it that you don't know how to detect the button press so that you can actually do something in response to the button being pressed?
Yes, that's right. I have three .h and .m files: appdelegate, the viewcontroller, and then a subview. What I'm trying to do is let the user change the image to a different image on the first page by using a button located on the subview. On the first view there's a label and a UIImageView (This imageview is the one i want to change). On the second view there's a UIButton. When this button is pressed, it should change the UIImageView on the first page.
The problem I've been having is letting the first page know that the button has been pressed, and changing the picture accordingly. Hope thats enough info for you to help. thanks!!
[edit to guy below me: sorry, I switched comptuers and couldnt edit the original post]

how to display image in image view with back and front options

im new to iphone now im working animals enclyclopidea. procedure is 6 image in 6 imageview when i click one image it comes full view and automaticaly go back in this we have to implement back and front options also.this is procedure.
so plz send me correct code for this animals enclyclopedia.
Thank u in advance.
you can invert your animations dictionary. If you have
[#"image1.png", #"image2.png", #"image3.png", nil];
then you programmatically set it to
[#"image3.png", #"image2.png", #"image1.png", nil];
and start animating again. And maybe you have to read the current image and set that as the first and calculate the others from this one.

Flashing UILabel as a

I'm trying to use a UIImage as a button which gives the impression of it being turned on then off again within about half a second. This works fine if I switch it on but if I want to switch it off again it doesn't switch on at all. I have a short loop in there to prevent it switching on and off so fast I can't see it but it doesn't switch on at all. I've tried it with and without the [flashingButton release]. Is there something I'm misunderstanding here? Can I addSubview and removeFromSuperView at the same time even with a short delay?
if ( some conditional statements in here .......) {
UIImage *estimateButton1 = [UIImage imageNamed:#"FlashingButton.png"];
flashingButton = [[UIImageView alloc] initWithImage:flashingButton1];
flashingButton.frame = CGRectMake (146,8,165,30);
[self.view addSubview:flashingButton];
// [flashingButton release];
// short loop in here to delay urning the button off
[self.flashingButton removeFromSuperview];
User interface drawing doesn't happen until later in the main run loop. Your call to addSubview adds flashingButton to self.view but doesn't draw it. Your short loop blocks the main run loop, so it still doesn't get to the drawing part. And then, you remove the button before the main run loop gets to draw it.
A solution is to let the main run loop continue after you've added the flashing button (so it will get drawn), but create a timer that will remove that button in the future. You can use performSelector:withObject:afterDelay: to do this.
[self.flashingButton performSelector:#selector(removeFromSuperview) withObject:nil afterDelay:0.5f];
You can read about run loops in "Threading Programming Guide" and about how drawing gets done in "View Programming Guide for iOS."
Looping within the main thread will just hang the program temporarily and prevent any drawing from taking place. Instead, use an NSTimer.

UIActionSheet is taking longer to load

are their any reasons why my UIActionSheet would be taking a while to load. It works fine just after lauch, but as soon as I loop through some actions once or twice, is loads very slow, and you can see the grey backdrop crawl down to finish drawing the sheet.
help?
Sam
Ok, so I found the solution moments later.
I assume this is how it works, feel free to correct me.
U used the cheats animation way of [ UIView ]
and i guess this changed the way for the UIActionSheet to pop up, so I wrote in these two lines before i asked it to show.in.view
- (IBAction)goToBlog{
contactUs.actionSheetStyle = UIActionSheetStyleDefault;
[UIActionSheet setAnimationDelay:0];
[UIActionSheet setAnimationDuration:0.1];
[contactUs showInView:self.view];
}
and that seemed to work. so im happy.