UIButton does not fire target on touchUpInside unless I drag just outside? - objective-c

I have a Button that when I press it, it does not fire the target selector that I've added. I've made the button Different Images, so I can see that I am Pressing it.
So here is the Funny if I Press, and drag outside of the button, but not outside of the button's immediate superView, the target selector is fired!
I've also in testing set the setClipsToBounds:YES on all the super/sub views just to be sure it was still in the views bounds. Seems to be within bounds. The dragging outside the button area seems to be omni directional, so its not like I can only tap/drag right. Left up and down work too. I can tap, drag out then back in and it works. If I don't start to drag and just tap and hold, the button highlights and then goes back to unselected state.
Here is the code for the Button. The Button is on a UIView along with a UITextView, Actually onTop Of the UITextView. The UIView that all of this is on is on a Larger View, which is on a Scaling/Scrolling view
messageLookupButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 21, 20)];
[messageLookupButton setTitle:#"junk" forState:UIControlStateNormal];
[messageLookupButton sizeToFit];
[messageLookupButton setTag:kGearButtonView];
//Get the images for the buttons
UIImage *gearButtonImage = [UIImage imageNamed:#"gear.png"];
UIImage *gearButtonImage2 = [UIImage imageNamed:#"StatusLightOn.png"];
[messageLookupButton setImage:gearButtonImage forState:UIControlStateNormal];
[messageLookupButton setImage:gearButtonImage2 forState:UIControlStateHighlighted];
[messageLookupButton addTarget:self action:#selector(gearPressed:) forControlEvents:UIControlEventTouchUpInside];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(processLookupResults:)
name:kRefreshFromPopover object:nil];
[self addSubview: messageLookupButton ];
[messageLookupButton release];
[messageLookupButton setCenter:CGPointMake(CGRectGetMaxX(elementViewContents.bounds) -kElementContentFrameOffset -(CGRectGetWidth( messageLookupButton.bounds)/2), CGRectGetMidY(elementViewContents.bounds))];
[self bringSubviewToFront:messageLookupButton];
The Scroll View has several gesture recognizers on it. Though they don't seem to interfere with other buttons and controls that I've put on the screen. Though I have a feeling its the Scroll View that is the problem.
scroll view code clip:
[scrollContentView setUserInteractionEnabled:YES];
[scrollContentView setExclusiveTouch:YES];
[scrollContentView setCanCancelContentTouches:YES];
[scrollContentView setDelaysContentTouches:YES];

I ended up Just adding the gear as an Image to the View, then created a invisible button that was the same size as the UIView that I placed the gear icon on.
-(void) addMessageLookupSelector {
// Set a default Color if there is none
if (_isMessageLookupField ){
// Add Gear to End Of Text Field
if (!messageLookupImageView) {
messageLookupImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 21, 20)];
[messageLookupImageView setTag:kGearButtonView];
[messageLookupImageView setImage:[UIImage imageNamed:#"gear.png"] ];
[self addSubview: messageLookupImageView ];
[messageLookupImageView release];
}
[self positionMessageLookupImageView ];
}
[self setNeedsDisplay];
}
- (void)makeControlEditable {
//Setup the TextField so we can edit its value, need to see if there is a Write Animation Assocated with Element
[self setTheWriteTag];
if (writeTag) {
writeInputRequestedButton = [UIButton buttonWithType:UIButtonTypeCustom];
[writeInputRequestedButton setFrame:elementViewContents.frame];
[writeInputRequestedButton setUserInteractionEnabled:YES];
[writeInputRequestedButton setEnabled:YES];
// [writeInputRequestedButton setBackgroundColor:[UIColor clearColor]];
[self addSubview: writeInputRequestedButton];
[self bringSubviewToFront:writeInputRequestedButton];
[writeInputRequestedButton addTarget:self action:#selector(wantsToEditValue:) forControlEvents:UIControlEventTouchUpInside];
}
}

Related

Why does my textField disappear?

I do some practice with iPhone4S about UITextField on iOS 7.1.2.
Interface:
The interface of the first version is simple, which just has a custom UITextField named MyUITextField and a UIButtom object that use to cancel the search operation. Inside the textField, its leftView property is initially set to a UIButton object, which background image is a magnifying glass. When users tap in the textField, that magnifying glass button will be removed and the leftView property will also set to a new UIButton object, which background image is a inverted triangle. When users tap the triangle button, app will create a new view object.
Main code:
//In this version,the textField's original frame property is set to (10,40,250,30)
//and the main operations are like this:
- (void)viewDidLoad
{
[super viewDidLoad];
self.myTextField.delegate = self;
self.myTextField.leftViewMode = UITextFieldViewModeUnlessEditing;
self.myTextField.leftView = [self creCustomSearchBar];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyBoardWillAppear:)
name:#"UIKeyboardWillShowNotification"
object:nil];
[self.cancleSearchButton addTarget:self
action:#selector(cancleSearch:)
forControlEvents:UIControlEventTouchUpInside];
}
/*when tapping in the textField,keyboard will appear*/
- (void)keyBoardWillAppear:(NSNotification *)notification
{
CGRect newLeftViewIndicatorRect = CGRectMake(20,5,20,20);
UIButton *newLeftViewIndicator = [[UIButton alloc] initWithFrame:newLeftViewIndicatorRect];
NSString *imagePath = [[NSBundle mainBundle] pathForResource:#"searchbar_textfield_down_icon#2x" ofType:#"png"];
[newLeftViewIndicator setBackgroundImage:[UIImage imageWithContentsOfFile:imagePath] forState:UIControlStateNormal];
[newLeftViewIndicator addTarget:self action:#selector(createActuralLeftView:) forControlEvents:UIControlEventTouchUpInside];
self.myTextField.leftView = newLeftViewIndicator;
/*in the second verson need to call */
//[self adjustViewFrame];
[self.cancleSearchButton setTitle:#"取消" forState:UIControlStateNormal];
}
- (void)createActuralLeftView:(UIButton *)leftViewButton
{
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self
action:#selector(tapRegionAboveKeyboard:)];
[self.view addGestureRecognizer:tapGesture];
NewLeftView *leftView = [[NewLeftView alloc] initWithFrame:CGRectMake(10, 80, 140, 100)];
leftView.delegate = self;
[self.view addSubview:leftView];
}
- (void)tapRegionAboveKeyboard:(UITapGestureRecognizer *)tapGesture
{
for (UIView *v in self.view.subviews) {
if ([v isKindOfClass:[NewLeftView class]]) {
[v removeFromSuperview];
[self.view removeGestureRecognizer:tapGesture];
return;
}
}
}
//in the second version need to call
- (void)adjustViewFrame
{
[[self.myTextField class] animateWithDuration:0.05 animations:^{
[self.myTextField setFrame:CGRectMake(self.myTextField.frame.origin.x, 40, self.myTextField.frame.size.width, self.myTextField.frame.size.height)];
}];
[[self.cancleSearchButton class] animateWithDuration:0.05 animations:^{
[self.cancleSearchButton setFrame:CGRectMake(self.cancleSearchButton.frame.origin.x, 40, self.cancleSearchButton.frame.size.width, self.cancleSearchButton.bounds.size.height)];
}];
}
In the first version things work well.
But in the second version, I set the textField frame property to (10,260,250,30), so I need to call the adjustViewFrame method in the keyBoardWillAppear: to reposition the textField in case of obscuring by the keyboard.
Here comes the problem: the textField's position is correctly moved to the right place, but when I tap the inverted triangle button, the textField disappeared.
I can't figure out what's going wrong here.

Event on an Image in iOS

I am pretty new to iOS development, so this may seem to be a stupid question.. In my xib file, i have taken a UIImageView that will show an image when the page is loaded. But when a user taps on an image, I want the next view to be loaded.. But I am not able to do that because we cannot have a delegate not a target-action mechanism for a UIImage view.. Many places, I found a workaround.. using a button and then placing an image on it, and then using it.. but I want the first way to work.. Please help..
use UITapGestureRecognizer ,and Don't forget to set imageView.userInteractionEnabled = YES; because by default image not having UserInteraction.
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleSingleTap:)];
[singleTap setNumberOfTapsRequired:1];
[imageView addGestureRecognizer:singleTap];
-(void)handleSingleTap:(id)sender {
// push you view here
}
(OR) If you want
take button with BackGround image.
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(270,10,30,30);
[button setBackgroundImage:[UIImage imageNamed:#"image.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(action:) forControlEvents:UIControlEventTouchUpInside];
Instead of Using UIImageView, you can use UIButton.
UIButton *myBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[myBtn setBackgroundImage:[UIImage imageNamed:#"your_image_Name"] forState:UIControlStateNormal];
[myBtn setBackgroundImage:[UIImage #"your_image_Name"] forState:UIControlStateHighlighted];
[myBtn addTarget:self action:#selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
also write button action:
-(void)buttonAction:(id)sender {
// Do whatever you want
}
In Case of XIB, Just use UIButton instead of UIImageView and set Background Image for both state Normal & Highlighted as you want. Rest you can manage button's action from XIB.
You can also add a fully transparent UIButton on top of your UIImageView... It should be preferred as each element has a clean semantics.

Adding UIPangesture for UIButton within UIScrollview to move over the all view

In my project i am having scroll at bottom and table at top.
Inside scrollview i am adding 10 buttons.for each button i had pan gesture to move around the screen.when the button intersect with table is will add a picture in tableview.
But the pan is working within the scroll.how to make it move over view
MY coding is:
downscroll=[[UIScrollView alloc]initWithFrame:CGRectMake(24, 635, 980, 100)];
downscroll.backgroundColor=[UIColor redColor];
downscroll.contentSize=CGSizeMake(990, 100);
[self.view addSubview:downscroll];
for(int i=1;i<=8;i++)
{
b1=[UIButton buttonWithType:UIButtonTypeCustom];
b1.frame=CGRectMake(30+px, 0, 80, 80);
[b1 setImage:[UIImage imageNamed: [NSString stringWithFormat:#"Icon%i.png",i]] forState:UIControlStateNormal];
[downscroll addSubview:b1];
// [self.view sendSubviewToBack:b1];
// [self.view bringSubviewToFront:b1];
[groupbutton addObject:b1];
panRecognizer3= [[UIPanGestureRecognizer alloc]initWithTarget:self action:#selector(move:)];
[b1 setUserInteractionEnabled:YES];
b1.exclusiveTouch=YES;
img.tag=i;
[b1 addGestureRecognizer:panRecognizer3];
px=px+120;
}
When i add my buttons directly without scroll scrollview it works fine.But i need to add button scrollview
please help with proper code. Thanks in advance.
Here is the way I used in my application
In my code I follow the below steps
In UIPanGestureRecognizer starting point I add the button to the self.view from scrollView
Drag button by using the translationInView method
3.In UIPanGestureRecognizer end state I'll check if the button dropped in the destination View.If so do your required task else add the button at the same position in the scrollView
`MoveControl` is a UIPanGestureRecognizer method
- (void)MoveControl:(UIPanGestureRecognizer *)recognizer
{
UIButton *vew=(UIButton *)[recognizer view];
CGPoint newCenter = [recognizer translationInView:self.view];
if (recognizer.state==UIGestureRecognizerStateBegan) {
CGPoint point=vew.frame.origin;
[vew setTitle:NSStringFromCGPoint(point) forState:UIControlStateSelected];
CGRect rect=[self.view convertRect:[vew frame] fromView:[vew superview]];
[vew setFrame:rect];
[self.view addSubview:vew];
CGPoint point1=vew.frame.origin;
[vew setTitle:NSStringFromCGPoint(point1) forState:UIControlStateDisabled];
}
else
{
CGPoint oldcentre= CGPointFromString([vew titleForState:UIControlStateDisabled]);
CGRect rect=vew.frame;
CGPoint origin=rect.origin;
origin.x=(newCenter.x+oldcentre.x);
origin.y=(newCenter.y+oldcentre.y);
rect.origin=origin;
vew.frame=rect;
if (CGRectIntersectsRect(rect, [pageOriginalContainer frame])) {
[YourTable setBackgroundColor:[UIColor lightGrayColor]];//Notifying that the tableView will accept the icon
}
else
{
[YourTable setBackgroundColor:[UIColor clearColor]];
}
}
if (recognizer.state==UIGestureRecognizerStateEnded)
{
CGRect rect=[vew frame];
if (CGRectIntersectsRect(rect, [pageOriginalContainer frame])) {
//your method of adding the Image to table
}
else//else part is means for if user dropped dragging somewhere else other than Table
{
CGPoint point=CGPointFromString([vew titleForState:UIControlStateSelected]);
CGRect frame=vew.frame;
frame.origin=point;
vew.frame=frame;
[pageCopyContainer addSubview:vew];
// [NSFileManager defaultManager]
}
}
}

iOS edit UIButton properties

I am working on the basics of iOS development.
So far I have a button that on press, will show some text. But what I want to do is after it is pressed, I want it to then change the text of the button, so far this is what I have:
- (void)viewDidLoad
{
[super viewDidLoad];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//button.backgroundColor = [UIColor redColor];
button.titleLabel.textColor=[UIColor blackColor];
button.frame = CGRectMake(25, 100, 275, 60);
[button setTitle:#"Press this button to reveal the text!" forState:UIControlStateNormal];
[button addTarget:self action:#selector(button_method:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)button_method:(UIButton *)sender {
NSString *test = #"I am learning Objective-C for the very first time! Also, this is my first ever variable!";
// handle button press
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(25, 25, 275, 60)];
label.text = test;
label.numberOfLines = 0;
label.lineBreakMode = UILineBreakModeWordWrap;
//label.lineBreakMode = NSLineBreakByWordWrapping; //iOS 6 only
[self.view addSubview:label];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
When I try adding [button setTitle:#"You pressed the button"]; to button_method
This doesn't work... why? And how would I make it work?
When I try adding [button setTitle:#"You pressed the button"]; to button_method, it doesn't work... why?
Because that method is nonexistent on UIButton.
And how would I make it work?
By using a method that UIButton actually responds to. For example:
[sender setTitle:#"You pressed the button" forState:UIControlStateNormal];
And read the relevant documentation, please.
Your code will not change the title of the button. But simply add a label as a subview for your view. Is this what you want?
If you want to change the text on the button you will want to do sender.title.text = test; in your button_method
I also recommend adding a NSLog(#"Button pressed"); to your button press method to double check that it is being called.

Button not clickable after adding as a subview to a UIView

I have created a class called UICustomButton, which is a subclass of UIView. I added a UIButton to UIView as a subview as shown in my code below:
-(id)initWithButtonType:(NSString *)type
{
self = [super init];
if (self)
{
self.customButton = [self setupButtonWithTitle:type andFrame:frame];
[self addSubview:self.customButton];
self.customButton addTarget:self action:#selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];
}
return self;
}
The problem I am facing is that although the button appears, they are not clickable. Is there something wrong with the way I am adding the buttons to the UIView?
EDIT: To add on, I am using this custom button class instance to a cell:
UICustomButton *customButton = [[UICustomButton alloc]initWithFrame:someFrame];
[cell.contentView addSubView:customButton];
check the frame of your UICustomButton object, and if self.customButton is out of its superView.
Make sure that the frame of the subview is within the frame of its superview. I encountered the issue twice and both times the frame of the subview was incorrect.
You should check if all properties userInteractionEnabled are on:
Check that property for UITableView where your cells with this view are displayed
Check that property for UITableViewCell where you add this view as subview
Check that property for UICustomButton
Check that property for customButton in -(id)initWithButtonType:(NSString *)type
My button was not working because i had two views in each other. The first view, as you can see, has a width and heigth of 0 pixels. I've made this view the same size as view2 and then my button was clickable.
UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(325, 346, 0, 0)];
view1.alpha = 0.90;
UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 450, 150)];
[view2.layer setCornerRadius:10.0f];
view2.backgroundColor = [UIColor whiteColor];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn addTarget:self action:#selector(addClosedToCollection) forControlEvents:UIControlEventTouchUpInside];
[btn setFrame:CGRectMake(30, 30, 300, 32)];
[btn setTitle:#"i'm a button" forState:UIControlStateNormal];
[view2 addSubview:btn];
[view1 addSubview:view2];
i hope i've helped somebody out :)
Your outer view probably has a height of 0. Add constraints that makes it the same height as (or higher than) the subviews, or create it with a frame that is large enough.
try putting this after the if statement:
self.isTouchEnabled = YES;