TVOS: UIButton action not working in subview - objective-c

in the TVOS Application storyboard mainview I added a subview.
Within the subview there are 3 button created in the very same way (the code here belongs to the subview)
UIButton *b = [UIButton buttonWithType:UIButtonTypeSystem];
b.frame = CGRectMake(left, top, 400, size);
[b setTitle:#"Click Me" forState:UIControlStateNormal];
[b addTarget:self action:#selector(Action_Up) forControlEvents:UIControlEventPrimaryActionTriggered];
[self.view addSubview:b];
...
-(void)Action_Up {
[self ShowData];
}
in the simulator and on the real device I can select the button and press it, but the action is never fired.
Could you help ?

Try this.. it worked for me
button.addTarget(self, action: "someMethodName:", forControlEvents: .PrimaryActionTriggered)

Related

iOS7 - Navigation Controller - Custom Back Button

I need to use custom Back button image for my new iOS 7 app. When I use Storyboard and add image by using Attributes inspector, this is what XCode renders for me:
Does anyone know why the Navigation controller behaves this way? I'm regularly putting #2x PNG image into Bar Button Item(under Navigation Item) in my Table View.
#interface MEDevicesViewController : UITableViewController
In my application, I use ECSlidingViewController and I have Table View within Navigation Controller. This is on my navigation controller.
Any help appreciated...
UIButton * button= [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectMake(0, 0, 51, 31)];
[button setImage:[UIImage imageNamed:#"back_btn.png"]forState:UIControlStateNormal];
[button addTarget:self
action:#selector(backbtnpressed:)
forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.leftBarButtonItem = barButton;
-(IBAction)backbtnpressed:(id)sender
{
[self.navigationController popViewControllerAnimated:YES];
}

Recognize swipe direction together with UIButton object

I need to recognize in which direction performed swipe and to know which object on my view has sent it. I've found workaround for now but it looks complicated and monstrous so I doubt is there any simplest solution?
My workaround in few words:
I attached recognizers to every UIButton objects on my view.
There is indicator that shows which button was tapped during performing swipe
I have to check this indicator and swipe direction for making right decision, which object and in which direction it should be moved.
Thank you in advise.
#synthesize swipeDirection; //label which shows swipe direction
#synthesize buttonNumber; //label which shows number of button which being tapped
- (void)viewDidLoad
{
UIButton* button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; //creating button
[button setTitle:#"test1" forState:UIControlStateNormal];
button.frame = CGRectMake(100, 100, 100, 100);
[button addTarget:self //adding selector which will update indicator which button been tapped, there isn't way to make swipe without tap button
action:#selector(updateButtonNumber:)
forControlEvents:UIControlEventTouchDown];
[button setTag:1];
[self.view addSubview:button]; //add button to view
[self beginListenToSwipes:button]; //send button to method which will add UISwipeGestureRecognizer for 4 directions
/*same task for second button*/
UIButton* button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button1 setTitle:#"test2" forState:UIControlStateNormal];
button1.frame = CGRectMake(200, 200, 100, 100);
[button1 addTarget:self
action:#selector(updateButtonNumber:)
forControlEvents:UIControlEventTouchDown];
[button1 setTag:2];
[self.view addSubview:button1];
[self beginListenToSwipes:button1];
}
/*every time when we tap button (begin swipe this method is called and buttonNumber always points to current button*/
-(void)updateButtonNumber:(UIButton*)sender
{
self.buttonNumber.text = [NSString stringWithFormat:#"%d",sender.tag];
}
/*method receives UIButton and add to it recognizers*/
- (void) beginListenToSwipes:(UIButton*)sender
{
UISwipeGestureRecognizer *recognizer;
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipe:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];
[sender addGestureRecognizer:recognizer];
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipe:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionLeft)];
[sender addGestureRecognizer:recognizer];
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipe:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionUp)];
[sender addGestureRecognizer:recognizer];
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipe:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionDown)];
[sender addGestureRecognizer:recognizer];
}
/*method which called when swipe performed and shows in which direction it was performed*/
-(void)handleSwipe:(UISwipeGestureRecognizer *)recognizer
{
int i = recognizer.direction;
self.swipeDirection.text = [NSString stringWithFormat:#"%d",i];
}
My idea would be,
Register for touchUpoutside event for UIButton.
When you move your finger out of the button it will be fired.
Find the touch point from touchesEnded method and store in a class level variable.
Compare the button frame and touched point in the touchupoutside method to find the direction of movement.
I think you could also put your gesture recognizer in self.view, and then see if the gesture took place over the control in question (obviously, you'd want to make your buttons ivars or properties of your viewcontroller so you can keep track of which is which), e.g.
CGPoint point = [sender locationInView:self.view];
if (CGRectContainsPoint(button1.frame, point))
// do whatever you want
It's up to you as to whether you do this logic at UIGestureRecognizerStateBegan or UIGestureRecognizerStateEnded or both.
Update:
By the way, if you're detecting which direction you're moving something, you can also contemplate using UIPanGestureRecognizer, which handles all four directions in a single gesture recognizer (and because it's a continuous, you can offer the user realtime feedback in the UI animating the move).

Selector is not called

I can't understand why the selector is not called.
//EDITED
[self.scrollView setContentSize:CGSizeMake(320, 600)];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn addTarget:self
action:#selector(validateTextFields:)
forControlEvents:UIControlEventTouchUpInside];
[btn setTitle:#"Продължи" forState:UIControlStateNormal];
btn.frame = CGRectMake(55, 580, 210, 50);
[self.scrollView addSubview:btn];
-(IBAction)validateTextFields:sender
{
NSLog(#"Called");
}
When I touch the button "Called" is not logged in console. If I change UIControlEventTouchUpInside to UIControlEventTouchDown validateTextFields method is executed.
You are adding a button on UIScrollView, this might be creating problem. Please check this question: iPhone: adding button to scrollview makes button inaccessible to interaction. It may be helpful.
The type for action should be
- (IBAction)validateTextFileds:(id)sender
{
// do your stuff
}
and your #selector parameter in addTarget should look like this: #selector(validateTextFields:)
try this:
[addContact addTarget:self action:#selector(buttonPressed:)forControlEvents:UIControlEventTouchUpInside];
-(IBAction)buttonPressed:(id)sender{
}

Capture dynamic button tap ios

I have button created programmatically in the UIImageView. This view was also created programmatically. When image in this view is tapped the button is created, now I want to capture when this button is tapped but I'm unable to here is my relevant code :
- (void)imageTapped:(UIGestureRecognizer *)sender{
MyImageView *myView = (MyImageView *)sender.view;
NSLog(#"Image tapped is => %#", myView.currentImageName);
//add button to view
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self
action:#selector(pressedPlay:)
forControlEvents:UIControlEventTouchDown];
//button image
UIImage *backgroundImage = [UIImage imageNamed:[NSString stringWithFormat:#"playbutton.png"]];
button.frame = CGRectMake(85.0, 175.0, backgroundImage.size.width, backgroundImage.size.height);
myView.userInteractionEnabled = YES;
button.userInteractionEnabled = YES;
[button setImage:backgroundImage forState:(UIControlStateNormal)];
[myView addSubview:button];
}
Now this code should handle the tap but for some reason it doesn't :
- (void)pressedPlay:(UIGestureRecognizer *)sender{
//MyImageView *senderView = (MyImageView *) sender.view;
NSLog(#"%#", #"I Presed button");
}
Any reason why this is not working?
Your code suggests that you are using a UIGestureRecognizer (most likely a UITapGestureRecognizer) to detect the initial tap. The problem is that UIGestureRecognizer is being greedy and stopping the UIButton from receiving the touch.
You will need to do some shifting to disable the gesture recognizer after the initial detection and then reenable it after the play has been tapped.
UIGestureRecognizer has the property
#property(nonatomic, getter=isEnabled) BOOL enabled
or you can choose to implement UIGestureRecognizerDelegate and provide an implementation of
gestureRecognizer:shouldReceiveTouch:
a very simple attempt could look like:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch;
{
return ![touch.view isKindOfClass:[UIButton class]];
}
and don't forget to set the delegate on the gesture recognizer
myGesture.delegate = self;
set the button frame from:
button.frame = CGRectMake(85.0, 175.0, backgroundImage.size.width, backgroundImage.size.height);
to:
button.frame = CGRectMake(0,0, backgroundImage.size.width, backgroundImage.size.height);
it looks like you are setting button frame outside the imageview's frame and hence the button is not actually getting any interaction.
The Button frame is likely setting the button outside the bounds of the UIImageView, frames are always relative to the superviews coordinate in this case the UIImageView.
[button setExclusiveTouch:YES];

How to change the image of the button on click of the button in objective c

I have placed a pause button in UI, where when the user clicks the button, the image of button from pause image it has to change to the start image. I have placed both the start and the pause images in the bundle, I am able to do this,but when i click on start again , it should show pause image button,How to do this, the following is the code i am using,
-(IBAction)btnClked:(id)sender
{
[pauseButton setImage:[UIImage imageNamed:#"pause.png"] forState:UIControlStateNormal];
UIButton *btn=(UIButton *)sender;
[btn setImage:[UIImage imageNamed:#"start.png"] forState:UIControlStateNormal];
}
You need a variable to keep track of that state change. A BOOL is fine.
But using your code, you can "abuse" the tag property of UIButton to tell if it's playing (tag == 1) or paused (tag == 0)
-(IBAction)btnClked:(id)sender {
UIButton *btn=(UIButton *)sender;
if (btn.tag == 1) {
btn.tag = 0;
[btn setImage:[UIImage imageNamed:#"pause.png"] forState:UIControlStateNormal];
} else {
btn.tag = 1;
[btn setImage:[UIImage imageNamed:#"start.png"] forState:UIControlStateNormal];
}
}
You can use UIButton's selected state to do the same. For example, while initiating the button you have to define 2 images. One for normal state and other for selected state. When touch event of the button fired, you have to make it selected. If it is already selected you need to deselect it. This is much better, than maintaining additional variables to check selected state of the button. Also you could improve the performance with this approach.
Code example
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIButton *startButton = [[UIButton alloc] initWithFrame:CGRectMake(200, 300, 60, 60)];
[startButton setBackgroundImage:[UIImage imageNamed:#"start.png"] forState:UIControlStateNormal];
[startButton setBackgroundImage:[UIImage imageNamed:#"pause.png"] forState:UIControlStateSelected];
[startButton addTarget:self action:#selector(modeButtonClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:startButton];
}
- (IBAction)modeButtonClick:(id)sender {
UIButton *startButton = (UIButton *)sender;
[startButton setSelected:![startButton isSelected]];
}
If you are creating the button from story board or XIBs set the backgroud image for default state config and selected state config.