Button on cameraOverlayView not working - ios

I have the following cameraOverlayView that is working to an extent. When the UIImagePickerController is called the standard controls are not shown and the 'Take Photo' button and logo.jpg are shown as described in ObViewControllerCameraOverlay. The button functions in that it goes blue when pressed but my problem is that it does not call the action (the NSLog is simply for test purposes). Can anyone see what I might have done wrong?
#implementation ObViewControllerCameraOverlay
- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame])
{
UIImage *logo = [UIImage imageNamed:#"logo.jpg"];
UIImageView *logoView = [[UIImageView alloc] initWithImage:logo];
logoView.frame = CGRectMake(0, 0, 240, 50);
[self addSubview:logoView];
UIButton *takePhotoButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[takePhotoButton setTitle:#"Take photo" forState:UIControlStateNormal];
takePhotoButton.frame = CGRectMake(0, 330, 320, 40);
[self addSubview:takePhotoButton];
}
return self;
}
- (IBAction)takePhotoButton:(id)sender
{
NSLog(#"test");
}

It doesn't look like you've told your button to call takePhotoButton: when it's tapped. Try adding
[takePhotoButton addTarget:self action:#selector(takePhotoButton:) forControlEvents:UIControlEventTouchUpInside];
after you've created the button in initWithFrame:

Related

UIButton in a subview not response to touch events with objective-c

I want to create a bottom tab that could change the view when user press.
To do that I create a view with view controller and sets it's frame in the init
Here is the bottom panel view controller
#implementation BottomTabViewController
-(id) initWithFrame:(CGRect)frame{
if(self = [super init]){
self.view.frame = frame;
return self;
}else{
return nil;
}
}
- (void)viewDidLoad {
[super viewDidLoad];
[self setUpButtons];
// Do any additional setup after loading the view.
}
-(void) featureBtnClick:(id*) sender{
NSLog(#"featureBtn Clicked");
}
-(void) favBtnClick:(id*)sender{
NSLog(#"Fav Btn Clicked");
}
-(void) setUpButtons{
UIButton *features = [[UIButton alloc]initWithFrame:CGRectMake(10, 10, 50, 50)];
[features setBackgroundImage:[UIImage imageNamed:#"feature.png"] forState:UIControlStateNormal];
features.backgroundColor = [UIColor greenColor];
[features addTarget:self action:#selector(featureBtnClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:features];
UIButton *favBtn = [[UIButton alloc]initWithFrame:CGRectMake([[UIScreen mainScreen]bounds].size.width - 100, 10, 50, 50)];
[favBtn setBackgroundImage:[UIImage imageNamed:#"favorite.png"] forState:UIControlStateNormal];
[favBtn addTarget:self action:#selector(favBtnClick:) forControlEvents:UIControlEventTouchUpInside];
favBtn.backgroundColor = [UIColor greenColor];
[self.view addSubview:favBtn];
}
#end
And I added that to my view with code like this:
-(void) setUpBottom{
BottomTabViewController *botm = [[BottomTabViewController alloc]initWithFrame:CGRectMake(0, [[UIScreen mainScreen] bounds].size.height - 55, [[UIScreen mainScreen]bounds].size.width, 55)];
botm.view.backgroundColor = [UIColor redColor];
// botm.view.userInteractionEnabled = false;
botm.view.userInteractionEnabled = true;
[self.view addSubview:botm.view];
}
Here here is the result seems, note the bottom panel that works, well pretty silly, but it shows up:
But when press the left and right button, there is not log printed which it expected to be to indict that the button works, What have I done wrong? Thanks
you are adding buttons in the view first then adding the bottom view.
if this is what you are doing then it can be overlapped by the bottom view.
In this case buttons cant be tapped because they have bottom view on top of them.
so make sure u add bottom view first then your buttons. (or change their frames to avoid overlapping)

Unable to Add Button

I am unable to add button on UIView. I have UIView In which I want to add UIButton. Here is my code which I am trying to implement.
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self setMultipleTouchEnabled:NO];
path = [UIBezierPath bezierPath];
[path setLineWidth:1.0];
_button = [UIButton buttonWithType:UIButtonTypeCustom];
[_button setTitle:#"" forState:UIControlStateNormal];
_button.userInteractionEnabled=YES;
_button.frame =CGRectMake(50, 130, 100, 100);
_button.backgroundColor =[ UIColor redColor];
[_button addTarget:self
action:#selector(buttonClicked)
forControlEvents:UIControlEventTouchUpInside];
[self addSubview:_button];
}
return self;
}
Your button is being added to the view. I just tried with your code line by line and I could see it. I think problem is in your UIView frame. Probably you are not seeing it because it is crossing the boundary of your super UIView.
Instead of assigning to __button, make a local UIButton object and work with it. After the local button has been added to the view, assign it to _button.

Moving a UIImageView with a UIButton

I am rather new to Xcode and iOS development but have been enjoying the challenge so far.
I have run into an issue where I am attempting to move a UIImageView I have created programmatically on top of a MapBox mapView.
I would like to move this UIImageView with a UIButton by a measure of pixels, with the UIButton being on top of the mapView.
This is the code I have so far in my ViewController.m:
[self.view addSubview:mapView];
UIImageView* ship;
ship=[[UIImageView alloc] initWithFrame:CGRectMake(150, 200, 40, 40)];
UIImage * image;
image=[UIImage imageNamed:#"spaceShip"];
[ship setImage:image];
ship.alpha = 0.75;
[self.view addSubview:ship];
UIButton *upButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
upButton.frame = CGRectMake(150, 250, 40, 40);
upButton.userInteractionEnabled = YES;
[upButton setTitle:#"UP" forState:UIControlStateNormal];
[upButton addTarget:ship action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:upButton];
}
- (IBAction)moveUp {
ship.center = CGPointMake(ship.center.x, ship.center.y -10);
}
Could anyone show me how to get this upButton to recognize and move my UIImageView?
One problem is that ship is a local variable that you create in the first block of code. When that code block goes out of scope, ship will be nil, so when you try to set it's center in the button method, it won't work. You need to create a property for ship, and use that instead.
Your other problem is that when you add the action to your button, you call it buttonPressed:, but the method you implemented is moveUp. So, if you create a property called ship, then your action method should be,
- (void)buttonPressed:(UIButton *) sender {
self.ship.center = CGPointMake(self.ship.center.x, self.ship.center.y -10);
}

Why the sender not called when I click the image of the button in Objective-C?

I am new to IOS and Objective-C , and I try to set Image for a UIButton.
Wen I click the button , the button will change image1. If I click the button one again , it will change to image2
First, I set the image in Viewdidload like the following code.
#interface AITPreviewViewController ()
{
VLCMediaPlayer *mediaPlayer ;
BOOL recording ;
BOOL recordMode;
}
- (void)viewDidLoad
{
recordMode = YES;
UIButton *modeChangeButton = [[UIButton alloc] initWithFrame:CGRectMake(200, 450, 60, 60)];
[modeChangeButton setBackgroundImage:[UIImage imageNamed:#"recordmode.PNG"] forState:UIControlStateNormal];
[self.view addSubview:modeChangeButton];
}
When I click the button , the button will change the image. The code is like the following:
- (IBAction)modeButtonClick:(id)sender {
NSLog(#"modeButtonClick..!!!!!!");
if (recordMode == YES) {
NSLog(#"modeButtonClick..%hhd = " , recordMode);
[self.modeChangeButton setBackgroundImage:[UIImage imageNamed:#"photomode.png"] forState:UIControlStateNormal];
recordMode = NO;
}else if (recordMode == NO){
NSLog(#"modeButtonClick..%hhd = " , recordMode);
[self.modeChangeButton setBackgroundImage:[UIImage imageNamed:#"recordmode.png"] forState:UIControlStateNormal];
recordMode = YES;
}
}
When I click the image on the button , the image of the button become dark in color. It seems the button has been press. But the - (IBAction)modeButtonClick:(id)sender didn't called.
When I click the blank space (the side of image) , the button has been press. The - (IBAction)modeButtonClick:(id)sender has been called. And the image also change to other image.
I am sure I have set action in header file and .m file.
The above describe is like the following picture.
It seems the button is cover by image , and the image and the button is not the same.
Why this happened ??
How to solve this problem ?
Thanks in advance.
Couple of changes that you could implement:
First:
Change the setBackgroundImage to setImage.
For example: [self.modeChangeButton setImage: [UIImage imageNamed: #"recordmode.png" forControlState: UIControlStateNormal];
That way, if you have set an image to the button by mistake, this will overwrite it.
Second:
Make sure you have connected your reference outlets (IBOutlet) and actions (IBActions) in Interface builder - whether XIB files, or storyboard. If you don't link the the interface objects there to those in your code, they won't work.
Third:
If it still does not work, try adding this line to your viewDidLoad
[self.modeChangeButton addTarget:self #selector(modeButtonClick:) forControlEvents:UIControlEventTouchUpInside];
Fourth:
Change this line in your viewDidLoad:
UIButton *modeChangeButton = [[UIButton alloc] initWithFrame:CGRectMake(200, 450, 60, 60)];
to:
self.modeChangeButton = [[UIButton alloc] initWithFrame:CGRectMake(200, 450, 60, 60)];
This should solve your problem.
This code is adding a new button on top of your self.modeChangeButton which I'm assuming is coming from a xib or storyboard.
- (void)viewDidLoad
{
recordMode = YES;
UIButton *modeChangeButton = [[UIButton alloc] initWithFrame:CGRectMake(200, 450, 60, 60)];
[modeChangeButton setBackgroundImage:[UIImage imageNamed:#"recordmode.PNG"] forState:UIControlStateNormal];
[self.view addSubview:modeChangeButton];
}
Do this instead...
- (void)viewDidLoad {
self.recordMode = YES;
}
- (void)setRecordMode:(BOOL)recordMode {
_recordMode = recordMode;
UIImage *imageForMode = recordMode ? [UIImage imageNamed:#"photomode.png"] : [UIImage imageNamed:#"recordmode.png"];
[self.modeChangeButton setBackgroundImage:imageForMode forState:UIControlStateNormal];
}
- (IBAction)modeButtonClick:(id)sender {
self.recordMode = !self.recordMode;
}
Check your button size and make the frame size closer to the image size. And try to maintain the two images as like below.
- (void)viewDidLoad
{
UIButton *modeChangeButton = [[UIButton alloc] initWithFrame:CGRectMake(200, 450, 60, 60)];
[modeChangeButton setImage:[UIImage imageNamed:#"recordmode.PNG"] forState:UIControlStateNormal];
[modeChangeButton setImage:[UIImage imageNamed:#"photomode.png"] forState:UIControlStateSelected];
[modeChangeButton addTarget:self action:#selector(modeButtonClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:modeChangeButton];
}
- (IBAction)modeButtonClick:(id)sender
{
sender.selected = !sender.selected;
}
Try modifying you viewDidLoad method as follows.
- (void)viewDidLoad
{
[super viewDidLoad];
UIButton *modeChangeButton = [[UIButton alloc] initWithFrame:CGRectMake(200, 300, 60, 60)];
[modeChangeButton setBackgroundImage:[UIImage imageNamed:#"recordmode.PNG"] forState:UIControlStateNormal];
// You might need to add following two lines into your code.
[modeChangeButton addTarget:self action:#selector(modeButtonClick:) forControlEvents:UIControlEventTouchUpInside];
self.modeChangeButton = modeChangeButton;
[self.view addSubview:modeChangeButton];
}
Above solution should work. As I see, you have referencing property named modeChangeButton in your modeButtonClick: method. But you are not assigning the created button into your property. Also you have to set the action selector to your button.
As a improvement, you can do this to improve performance of your application. Without setting the background image on click event, you can select/deselect the button. With this you don't need to maintain boolean flags, which may introduce new bugs to your application.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIButton *modeChangeButton = [[UIButton alloc] initWithFrame:CGRectMake(200, 300, 60, 60)];
[modeChangeButton setBackgroundImage:[UIImage imageNamed:#"recordmode.png"] forState:UIControlStateNormal];
[modeChangeButton setBackgroundImage:[UIImage imageNamed:#"photomode.png"] forState:UIControlStateSelected];
[modeChangeButton addTarget:self action:#selector(modeButtonClick:) forControlEvents:UIControlEventTouchUpInside];
self.modeChangeButton = modeChangeButton;
[self.view addSubview:modeChangeButton];
}
- (IBAction)modeButtonClick:(id)sender {
NSLog(#"modeButtonClick..!!!!!!");
[self.modeChangeButton setSelected:![self.modeChangeButton isSelected]];
}
Hope this helps.

adding multiple subviews in ios

I'm trying to add multiple subview programmatically, but my buttons inside those subviews are not working. The buttons were also added as subviews programmatically.
Here's the code, hopefully will explain more about what I mean.
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
[self setBackgroundColor:[UIColor clearColor]];
// UIView container of selected image with comment button.
UIView *containerOfSelectedImage = [[UIView alloc] initWithFrame:CGRectMake(0, 20, 0, 350)];
NSLog(#"%f", self.frame.size.width);
// image view of selected photo by user.
UIImageView *userImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"nature_01.jpg"]];
[userImage setFrame :CGRectMake(0, 0, 340, 300)];
// comment button with action of recodering user's comment.
UIButton *commentButton = [UIButton buttonWithType:UIButtonTypeCustom];
//[commentButton setBackgroundColor : [UIColor clearColor]];
float y = userImage.frame.size.height + 5.0f;
[commentButton setFrame : CGRectMake(32.0f, y, 24.0f, 24.0f)];
[commentButton addTarget:self action:#selector(audioRecordAction) forControlEvents:UIControlEventTouchDown];
[commentButton setImage:[UIImage imageNamed:#"comments-24.png"] forState:UIControlStateNormal];
[containerOfSelectedImage addSubview:userImage];
[containerOfSelectedImage addSubview:commentButton];
[self addSubview:containerOfSelectedImage];
[self addSubView:self.commentContainerView];
return self;
You need to increase the frame of the containerOfSelectedImage. Currently the width is 0.
The containerOfSelectedImage's frame has to be big enough so the button fits inside it. If the button is outside that frame it will show up but you can't touch him.
To debug this issues you can use a tool like Reveal App. If any of your buttons is outside the frame of the parent then the touch will not be detected.
Change [commentButton addTarget:self action:#selector(audioRecordAction) forControlEvents:UIControlEventTouchDown]; to [commentButton addTarget:self action:#selector(audioRecordAction) forControlEvents:UIControlEventTouchUpInside];

Resources