Pugalmuni
Pugalmuni

Reputation: 9390

How can I write the actions for image view when touch it in iPhone-OS 4.0

I want to display the image in navigation bar instead of a back button with an action. So now I have used image view and displayed the image in the navigation bar. Now I want to write the actions for image view, when touch or click the image view. Is it possible to write an action for image view?

I am currently working in iPhone OS 4.0.

Please see my previous question.

Here is my code:

     UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(0,5,40,40)];
     [self.navigationController.navigationBar addSubview:view1];
     view1.backgroundColor = [UIColor clearColor];

     UIImage *img = [UIImage imageNamed:@"Back.png"];
     UIImageView *imgView = [[UIImageView alloc] initWithImage:img];
     [view1 addSubview:imgView];

How can I write the actions for image view?

Upvotes: 0

Views: 2002

Answers (2)

tc.
tc.

Reputation: 33602

Don't add views directly to the navigation bar unless you're prepared for it to break in a future OS release.

UIButton * b = [UIButton buttonWithType:UIButtonTypeCustom];
b.image = [UIImage imageNamed:@"Back.png"];
b.frame = (CGRect){{0,0},{40,40}};
[b addTarget:self action:@selection(wahatever) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:b] autorelease];

Upvotes: 0

Eiko
Eiko

Reputation: 25632

Before iOS 3.2, nou would need a sublass of UIImageView and implement the touch handlers. Or replace your UIImageView with a UIButton with custom type.

With iOS 3.2+, UIGestureRecognizer is what gets your things done efficiently - just add the appropriate one to your view.

Upvotes: 2

Related Questions