Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 10-07-2008, 01:21 PM   #1 (permalink)
Registered Member
 
Join Date: Aug 2008
Location: Ireland
Age: 21
Posts: 472
Question Make UIImageView receive actions

Hey everyone. I have a UIImageView that I want to receieve an action. I want it to act like a button. It will not have an image set so it will be invisible, then when tapped i want it to change to an image.

I know that IBActions do not work on a UIImageView. How could I achieve this?


P.S I don't want to set the image property of a button it must be a UIImageView.
__________________
On the iOS App Store
kieran12 is offline   Reply With Quote
Old 10-07-2008, 04:27 PM   #2 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
Default

Quote:
Originally Posted by kieran12 View Post
Hey everyone. I have a UIImageView that I want to receieve an action. I want it to act like a button. It will not have an image set so it will be invisible, then when tapped i want it to change to an image.

I know that IBActions do not work on a UIImageView. How could I achieve this?


P.S I don't want to set the image property of a button it must be a UIImageView.
UIImageView inherits from UIControl - so look there to see how it can respond.

As far as I can see, you should not have to subclass UIImageView - just sent add another object as the target for any UIControlEventTouchUpInside events, using "addTarget:action:forControlEvents:"

iPhone Dev Center

Something like this: (Danger, I have not compiled this. Watch for typos.)
Code:
[myImageView addTarget:myController action:@selector(myAction:) forControlEvents:UIControlEventTouchUpInside];
myController should be an object that has a method "myAction: (id)sender" .
__________________

Free Games!

Last edited by smasher; 10-07-2008 at 04:43 PM. Reason: fix my @selector directive
smasher is offline   Reply With Quote
Old 10-07-2008, 04:43 PM   #3 (permalink)
Registered Member
 
Join Date: Aug 2008
Location: Ireland
Age: 21
Posts: 472
Default

Thanks, I'll give it a try!
__________________
On the iOS App Store
kieran12 is offline   Reply With Quote
Old 05-17-2009, 02:53 PM   #4 (permalink)
Registered Member
 
Join Date: Apr 2009
Posts: 118
Default

Hi Guys,

There may be a technical reason why this does not work... but you can use a normal button and set it's type (top of the inspector) to custom, then just set your image to the BG.

Alex
lexy0202 is offline   Reply With Quote
Old 05-17-2009, 02:59 PM   #5 (permalink)
New Member
 
RSO Software's Avatar
 
Join Date: Mar 2009
Posts: 28
Default

Quote:
Originally Posted by kieran12 View Post
Hey everyone. I have a UIImageView that I want to receieve an action. I want it to act like a button. It will not have an image set so it will be invisible, then when tapped i want it to change to an image.

I know that IBActions do not work on a UIImageView. How could I achieve this?


P.S I don't want to set the image property of a button it must be a UIImageView.
Replace it with a button in the drawRect statement.

Code:
- (void)drawRect:(CGRect)rect {

	myButton = [UIButton buttonWithType:UIButtonTypeCustom];
	[myButton setImage:[UIImage imageNamed:@"Image Name.png"] forState:UIControlStateNormal];
	middleControllerButton.frame = CGRectMake(x, y, width, height);
	[myButton addTarget:self action:@selector(myAction) forControlEvents:UIControlEventTouchUpInside];
	[self addSubview:myButton];

}
__________________
-Alec

iTaco
RSO Software is offline   Reply With Quote
Old 05-17-2009, 03:03 PM   #6 (permalink)
Registered Member
 
Join Date: Aug 2008
Location: Ireland
Age: 21
Posts: 472
Default

I started this thread a long time ago now. Forgot all about it. In the end I just used a UIImageView and the touchesBegan/touchedEnded methods.
__________________
On the iOS App Store
kieran12 is offline   Reply With Quote
Old 05-17-2009, 04:56 PM   #7 (permalink)
Objective-C Code Monkey
 
deansx's Avatar
 
Join Date: May 2009
Location: Mountain View, CA
Posts: 127
Default

Quote:
Originally Posted by kieran12 View Post
Hey everyone. I have a UIImageView that I want to receieve an action. I want it to act like a button. It will not have an image set so it will be invisible, then when tapped i want it to change to an image.

I know that IBActions do not work on a UIImageView. How could I achieve this?


P.S I don't want to set the image property of a button it must be a UIImageView.
The default value for the userInteractionEnabled property of a UIImageView is NO. To make a UIImageView accept actions, you need to set the value of the property to YES.
Code:
[myUIImageView setUserInteractionEnabled:YES];
deansx is offline   Reply With Quote
Old 02-20-2010, 05:38 AM   #8 (permalink)
Registered Member
 
Join Date: Oct 2009
Posts: 5
Default Can't do it yet...

I could use Touch events of the UIImageView programmartically, use a transparent button over the image, use a custom button with the image, etc.

However, I can't simply use the UIImageView with UserInteractionEnabled inside the Interface Builder and simply wire it to an IBAction. There is no IBAction I can wire the UIImageView to. Nor can I set the Class of it to UIControl. (Like for the background, I can change the class to UIControl and wire it to backgroundTouched action).

Since the UIImageView must have desended from UIVIew and UIView can be a UIControl, this does not make much sense, but that is how it is in the Interface Builder.

Anyone has any idea how to?
yoichi is offline   Reply With Quote
Reply

Bookmarks

Tags
actions, button, events, uiimageview

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 276
20 members and 256 guests
ADY, AragornSG, BrianSlick, Dani77, Dattee, dre, glenn_sayers, HDshot, HemiMG, JasonR, karlam963, nobre84, prchn4christ, Rudy, spiderguy84, themathminister, tomtom100, viniciusdamone, vogueestylee, vvenkatachallam
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,884
Threads: 89,229
Posts: 380,763
Top Poster: BrianSlick (7,129)
Welcome to our newest member, karlam963
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 02:19 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0