Handling button actions - ios

I have 3 buttons named as : bw1 , bw2 and bw3
Simply, I want a specific action to be applied when one of these buttons is clicked using hidden.
Each button should only hide itself when clicked.
My problem is when bw3 is clicked, both bw3 and bw2 are hidden. But when I click bw2, nothing wrong happened which means that the problem is in bw3 method.
I don't know what the problem is. I traced the code many times but I could not find the solution.
- (IBAction)bw1:(id)sender {
_lw1.hidden = NO;
_bw1.hidden = YES;
}
- (IBAction)bw2:(id)sender {
_lw2.hidden = NO;
_bw2.hidden = YES;
}
- (IBAction)bw3:(id)sender {
_lw3.hidden = NO;
_bw3.hidden = YES;
}

As per my comment above
check your xib, for _bw3, there are 2 IBAction set, bw2 and bw3.
i.e. - (IBAction)bw2:(id)sender and - (IBAction)bw3:(id)sender
Hope it helps you..

Related

UITapGestureRecognizer Not Getting Called

I am adding the following UITapGestureRecognizer to my view.
- (void)viewDidLoad {
_focusView = [[PBJFocusView alloc] initWithFrame:CGRectZero];
_focusTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(_handleFocusTapGesterRecognizer:)];
_focusTapGestureRecognizer.delegate = self;
_focusTapGestureRecognizer.numberOfTapsRequired = 1;
_focusTapGestureRecognizer.enabled = NO;
[_previewView addGestureRecognizer:_focusTapGestureRecognizer];
}
- (void)_handleFocusTapGesterRecognizer:(UIGestureRecognizer *)gestureRecognizer {
NSLog(#"Focus");
}
_focusTapGestureRecognizer should be enable. Try this
_focusTapGestureRecognizer.enabled = YES;
OR
Remove this line
_focusTapGestureRecognizer.enabled = NO;
Try this inside your code
_focusView.userInteractionEnabled = YES;
May be helpfull...
_focusTapGestureRecognizer.enabled = NO;
Remove this line
To make this work check these three things as it is suggested by other guys i only writing them step by step
Check did you set the delegate of TapGesture in .h file.
Check on the UIView you are adding gesture its userInteractionEnabled is True.
Make that enable of UITapGesture Yes .//Why it is no ?
Fourth is , did you gave any size to the UIView , for checking that add some background color to it,see is it visible or not .
if these 4 doesnot help you out type your code what you used it .
You might also want to check that you're not trying to reuse the UITapGestureRecognizer object for different views.
It seems like you can because you add the gesture to the view, but in ultimately the view needs to be assigned to the view property on the gesture object so adding the gesture object to a second view will just overwrite the first.

Is there a way to enable parallax for a Form Sheet on iPad?

thanks for turning up :-)
I noticed that on the iPad the Form Sheet view does not have any iOS 7 parallax effects, and I would actually like to incorporate this because I think it kinda looks cool.
Well this is what I have and it doesn't have that, so is there a way to do this? How would one go about doing it? I've googled and googled and google has finally failed me because nothing appropriate is appearing.
Thanks :)
This can be done with some effort.
First off, you need to know how to add parallax to a view in general:
I have the following category method for UIView to make it easy:
- (void)addDepthMotionX:(CGFloat)x y:(CGFloat)y {
Class clazz = NSClassFromString(#"UIInterpolatingMotionEffect");
if (clazz) {
BOOL reverse = NO;
if (CGAffineTransformEqualToTransform(self.transform, CGAffineTransformMakeRotation(-M_PI_2)) || CGAffineTransformEqualToTransform(self.transform, CGAffineTransformMakeRotation(M_PI_2))) {
reverse = YES;
}
UIInterpolatingMotionEffect *eff = [[clazz alloc] initWithKeyPath:#"center.x" type:reverse ? UIInterpolatingMotionEffectTypeTiltAlongVerticalAxis : UIInterpolatingMotionEffectTypeTiltAlongHorizontalAxis];
eff.maximumRelativeValue = #(x);
eff.minimumRelativeValue = #(-x);
[self addMotionEffect:eff];
eff = [[clazz alloc] initWithKeyPath:#"center.y" type:reverse ? UIInterpolatingMotionEffectTypeTiltAlongHorizontalAxis : UIInterpolatingMotionEffectTypeTiltAlongVerticalAxis];
eff.maximumRelativeValue = #(y);
eff.minimumRelativeValue = #(-y);
[self addMotionEffect:eff];
}
}
The code is guarded so it won't crash if called from other than iOS 7 (or later).
Now to add parallax to a view you simply do:
[someView addDepthMotionX:10 y:10]; // pick an appropriate depth value
The final step to your question is to apply this to the root of the view controller you are displaying.
Here's code you can add in the viewWillAppear: method of the view controller. Adjust to meet your needs:
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
if (self.isBeingPresented || self.isMovingToParentViewController) {
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad && self.modalPresentationStyle == UIModalPresentationFormSheet) {
[self.navigationController.view.superview addDepthMotionX:15 y:15];
}
}
}
One issue with this code is if the user rotates the iPad 90 degrees after the form sheet is presented, the parallax effect needs to be updated. But this will get you started.

Stuck with UIActionSheetPicker , iOS

This is going to be a very targeted question , as its for people that have actually used the UIActionSheetPicker before.
I used it in my iphone applications and everything worked great , but now that i tried to implement it on my ipad i experienced some problems.
The main problem is that on ipad the picker appears as a "bubble" or "info window" if you prefer , which points at the spot where it was called , like a button a text field etc.
Below is an example of it :
You can see that the "info window" is pointing at the animals button.
In my application i have 4 different buttons. 2 are in the top side of the screen and 2 at the bottom.
Those who are at the bottom , call the picker very well and the picker appears on top of the buttons pointing down on them.
However , the ones on the top of the screen (almost like the one in the image) when they call the picker the picker appears much lower on the screen and doesnt point at them as it should ...
I mean i expected to appear just under the buttons pointing at them (like in the image), but they are almost in the center of the screen pointing nowhere..
Any ideas? Has someone experienced this before?
EDIT
In the beginning , i was calling the ActionSheetPicker inside the buttons action like this:
- (IBAction)selectTopic:(UIControl *)sender {
[ActionSheetStringPicker showPickerWithTitle:NSLocalizedString(#"CHOOSE_TOPIC", nil) rows:self.topics initialSelection:self.selectedIndex target:self successAction:#selector(topicWasSelected:element:) cancelAction:#selector(actionPickerCancelled:) origin:sender];
//when user picks a new topic we clean the titleField to make sure selected title of previous topic doesnt mix up with new topic
titleField.text = #"";
}
Now i am trying to call it like this:
- (IBAction)selectTopic:(UIControl *)sender {
ActionSheetStringPicker *thePicker = [[ActionSheetStringPicker alloc] initWithTitle:NSLocalizedString(#"CHOOSE_TOPIC", nil) rows:self.topics initialSelection:self.selectedIndex target:self successAction:#selector(topicWasSelected:element:) cancelAction:#selector(actionPickerCancelled:) origin:sender];
thePicker.presentFromRect = topicField.frame;
[thePicker showActionSheetPicker];
//when user picks a new topic we clean the titleField to make sure selected title of previous topic doesnt mix up with new topic
titleField.text = #"";
}
Where topicField is my TextField.
Sadly the result is the same. Even now that i am specifying where i want the arrow to point , the picker is called 300 pixels down.
The strange thing is though that even with another other button that is a bit lower than the previous the picker is again exactly 300 pixels down.
EDIT2
After noticing that the picker shows 300 pixels down , i decided to manually make it show 300pixels up , to point exactly on my button.
I used the following code :
- (IBAction)selectTopic:(UIControl *)sender {
//[ActionSheetStringPicker showPickerWithTitle:NSLocalizedString(#"CHOOSE_TOPIC", nil) rows:self.topics initialSelection:self.selectedIndex target:self successAction:#selector(topicWasSelected:element:) cancelAction:#selector(actionPickerCancelled:) origin:sender];
ActionSheetStringPicker *thePicker = [[ActionSheetStringPicker alloc] initWithTitle:NSLocalizedString(#"CHOOSE_TOPIC", nil) rows:self.topics initialSelection:self.selectedIndex target:self successAction:#selector(topicWasSelected:element:) cancelAction:#selector(actionPickerCancelled:) origin:sender];
CGRect pickerFrame = topicField.frame;
pickerFrame.size.height -= 300;
thePicker.presentFromRect = pickerFrame;
[thePicker showActionSheetPicker];
//when user picks a new topic we clean the titleField to make sure selected title of previous topic doesnt mix up with new topic
titleField.text = #"";
}
Amazingly the button once again appears in the same position 300pixels down. I start to believe that this one may not be the property to alter the position of the picker.
Any ideas ?
Setting the presentFromRect in your calling code won't make a difference since it will be reset based on the sender within the ActionSheetPicker code, instead you are going to need to modify the source code of the library.
The following (unmerged) commit on github should resolve the issue On iPad, set the popover to point properly at its container.
Basically within the AbstractActionSheetPicker.m file modify the - (void)presentPickerForView:(UIView *)aView method like the following
- (void)presentPickerForView:(UIView *)aView {
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
self.presentFromRect = CGRectMake(0, 0, self.containerView.frame.size.width, self.containerView.frame.size.height);
[self configureAndPresentPopoverForView:aView];
}
else {
self.presentFromRect = aView.frame;
[self configureAndPresentActionSheetForView:aView];
}
}
After a brief look at the code, I would say that you want to make sure that the presentFromRect on the ActionPicker object is in the coordinates of the container that you are passing in on the init. For instance if I had one big view that contained a button:
This is untested:
UIView* someBigView = ... // get the big view
UIButton* someButton = ... // get the button that the big view contains
ActionSheetDatePicker* thePicker = [[ActionSheetDatePicker alloc] initWithTitle:#"Some Title"
datePickerMode:UIDatePickerModeDate
selectedDate:[NSDate date]
target:self
action:#selector(someMethod:)
origin:someBigView];
//the arrow should be pointing at the button
thePicker.presentFromRect = someButton.frame;
[thePicker showActionSheetPicker];
In my opinion, this library has a pretty big flaw in the fact that it takes an origin parameter that can be a button item or a container. It requires that you look at the source to understand it, instead of being (somewhat) obvious from the interface.

How to access multiple buttons in access function?

I have two buttons in each row of a tableview. One is labeled "have it" the other "want it" Each button starts off at 20% opacity when the app starts. When one button is tapped the opacity is set to 100% . I need logic so that if one button is set to 100% opacity and the other one set at 20% is tapped, the first button needs to be set to 20% and the second button to 100% (so the opacity needs to be reversed).
Each button has it's own action that is run when pressed. I can access the button that is pressed and set the opacity with (UIButton *senderButton = (UIButton *)sender). However I need to set the opacity of the other button as well. How can access the other button (the one that was not pressed) inside of my action/function that is called when one is pressed? Thanks!
You can create an outlet for each button. So that you can set its property from any where within its container class.
if I correct understand your question, you can declare your buttons in header-file like this:
#interface myController : UIViewController
{
UIButton *b1;
UIButton *b2;
}
tmen in m-file (in viewDidLoad) you can set this buttons with one selector and different tags: (for more information about creation buttons: How do I create a basic UIButton programmatically?)
-(void)viewDidLoad
{
[super viewDidLoad];
b1 = [UIButton buttonwithType:UIButtonTypeCustom];
[b1 addTarget:self withAction:#selector(clickINMyButtons:) forState:UIControlTouchUPInside]; // sorry, I don't remember correct syntax, i'll correct this some later if you needed in it.
b1.tag = 1;
b1.frame = CGRectMake(0,0,12,12); //example
[self.view addSubView:b1];
}
alike declare b2 with different:
b2.tag = 2;
So, then you implement your selector with changing opacity:
-(void)clickINMyButtons:(UIButton *)sender
{
if (sender.tag == 1)
{
sender.alpha = 1; // or b1.alpha = 1;
b2.alpha = 0.2;
}
else if (sender.tag == 2)
{
sender.alpha = 1; // or b2.alpha = 1;
b1.alpha = 0.2;
}
}

How do I disable a UIButton?

I am working on a project in which I have to show all photos of Photo Library in a plist and show them horizontally on UIButtons.
My app will also have an edit button: when the user clicks this button, a delete mark (such as usually appears in other iPhone/iPad apps) should show on each button.
But here's the crucial bit: as soon as this delete mark appears, the functionality of the button should be disabled. I've tried accomplishing this with the following:
{
editbutton.enabled=NO;
}
...but it neither produces an error nor works. What should I do?
Please set this...
editButton.userInteractionEnabled = NO;
or You can use
editButton.enabled = NO;
Swift 3 and above
editButton.isEnabled = false
setEnabled is now part of the setter method for isEnabled.
setter for property enabled is overridden in class UIButton. try to send a message.
[editbutton setEnabled:NO];
Use the enabled property of UIControl which the super class of UIButton and set it with NO.
myButton.enabled = NO;
You could also try as #Marvin suggested,
In that case your button will not respond to any touch event from user,
In addition to the above:
Set
editButton.userInteractionEnabled = NO;
or you can use
editButton.enabled = NO;
You might want to gray out the button so the user knows it was disabled:
editButton.alpha = 0.66
yourBtn.userInteractionEnabled = NO;

Resources