UIButton press triggers method action - objective-c

I have PhotoView class (subclass of UIButton) and I would like that when I press any button that is class of PhotoView, same action should be triggered.
I tried something like:
- (void)didSelectButton:(PhotoView *)sender
{
// do something
}

In your PhotoView init... method, set self as its own target:
[self addTarget:self selector:#selector(didSelectButton:) forControlEvents: UIControlEventTouchUpInside];
Please note that this is a poor design. Your PhotoView should be a UIView that contains a UIButton.

in the viewDidLoad method of your view controller you could iterate on your view subviews (and their own subviews, recursive style) in order to found instances of your PhotoView class and use addTarget on those instances.

Related

Capture my uibuttons event

I have a big problem in my iphone/ipad ios7 app, I have a lot of controls based on UIView.
for example
list in my view controller I'm adding list based on UIView, this list contains some controls subviews based on UIView, and this controls have a lot subviews (particulary uibuttons) too. And now I want get UIControlEventTouchUpInside action im my viewcontroller, how I can do that ? I Do delegate im my uibutton control but im my view controller I dont't have instance this button, so I can't use
myButton.delegate = self;
I Have just instance my SuperView.
Someone could help me?
It will be better if you use custom view class for your UIView which is you used for containing your button and other controls. In that custom class you can simply set the action for your controls.
EDIT
You can use the following method in your UIButton's custom class:
- (void)awakeFromNib {
[super awakeFromNib];
[self addTarget:self
action:#selector(yourClickMethod:)
forControlEvents:UIControlEventTouchUpInside];
}
- (void)yourClickMethod:(UIButton *)infoButton {
// Button action goes here
}

How to specify the segue programmatically (NOT perform segue)?

AView.m
UIButton *btn = [[UIButton alloc]initWithFrame()];
[btn addTarget:self action:#selectior(perpareForSegue:sender:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
//I want to get destinationViewController of segue with identifier"toNextView"
// and then perform the segue
// Difficulties that I encounter is that I have a NSInvalidArgumentException
// Is that mean the view controller doesn't know the existence of the segue?
// I have connected two controller with a segue
}
I want to get destinationViewController of segue with identifier"toNextView"
and then perform the segue
Difficulties that I encounter is that I have a NSInvalidArgumentException
Is that mean the view controller doesn't know the existence of the segue?
I have connected two controller with a segue.
Take a look at the class reference of UIStoryboardSegue.
The method prepareForSegue:sender: is called by the storyboard runtime when a segue is triggered. It is not meant to be called to perform the segue.
Here is the explanation from the class reference:
When a segue is triggered, but before the visual transition occurs,
the storyboard runtime calls the current view controller’s
prepareForSegue:sender: method so that it can pass any needed data to
the view controller that is about to be displayed.
From the class reference:
You can still initiate a segue programmatically using the
performSegueWithIdentifier:sender: method of UIViewController if you
want. You might do so to initiate a segue from a source that was added
programmatically and therefore not available in Interface Builder.
So, just specify the selector for the button and then inside the selector just call performSegueWithIdentifier:sender: method.
[btn addTarget:self action:#selector(whateverAction) forControlEvents:UIControlEventTouchUpInside];
- (void)whateverAction {
[self performSegueWithIdentifier:#"toNextView" sender:nil];
}
And if you want to pass an object to the destination view controller of the segue, that's where prepareForSegue:sender: method comes into play as explained in the class reference, for example:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"toNextView"]) {
DestinationViewController *vc = (DestinationViewController *)[segue destinationViewController];
//do whatever you want to do with vc, including passing of data, setting property, etc.
}
}

calling an method in a view controller from a child view

In my ViewController I have created a view which contains a button and a bunch of other elements. When the button is pressed I want to call a method from the parent ViewController. I tried:
[self.superview buttonPressedMethod];
But the superview is no the ViewController but UIView. Is there anyway to do this?
This is exactly what the target/action mechanism is for. Make the method in the view controller an IBAction. Set the view controller as the button's target, and set the method as its action. That should be all you need to do.
At ParentViewController.h
-(void)ParentMethod;
At ParentViewController.m
-(IBAction)button:(id)sender {
[self ParentMethod];
}
At IBAction of Play Button on child view controller do this:
-(IBAction)ChildMethod:(id)sender {
ParentViewController *parent=self.parentViewController;
[parent ParentMethod];
}
Better create delegate in view controller and set delegate in child view, so when something occur call delegate method according to it.

Calling a method in a UIViewController from a UIButton in a subview

Still learning about Objective C and getting the structure right.
I have an iOS App with a UIViewController that has a defined method named "doSomething". In my view controller I have a view and in that view a number of UIButton that I create programmatically (see example below with one button).
Now when I press the button I want to call my method "doSomething". The way I currently do it is like this:
[myButton addTarget:nil
action:#selector(doSomething:)
forControlEvents:UIControlEventTouchUpInside];
Since my target is nil it goes up the responder chain until it finds a method called "doSomething". It works, but it does not really feel right.
I have started to look into using #protocol but not really got my head around it. I have been looking at some tutorials but for me it is not clear enough. I have used protocols like for the table view controllers, but defining one is new for me.
Would it be possible to get an example for this specific case?
Thanks!
As your target pass in the view controller and the method will be called on that object.
Edit:
[myButton addTarget:controller
action:#selector(doSomething:)
forControlEvents:UIControlEventTouchUpInside];
Assuming that you have a variable called controller that is your UIViewController. If you don't have a reference to your controller then simply pass one to your view.
Edit2:
View interface:
#property (assign) UIViewController* controller;
View implementation:
#synthesize controller;
Controller:
- (void) viewDidLoad {
[super viewDidLoad];
someView.controller = self;
}
The way I'd do it is set the value of addTarget to self.
[myButton addTarget:self
action:#selector(doSomething:)
forControlEvents:UIControlEventTouchUpInside]
This will look for the method doSomething on the object that the target is added in. In this case, this would be the view controller.

keyup keydown methods NSViewController

I made an instance of NSViewController and added it as a subview to the main window's content view. I want to be able to capture keyboard events, but I have no idea how to implement it.After some research, I learned I needed to implement acceptsFirstResponder and the keyUp:event: and keyDown:event: methods in the NSViewController, but after that I still don't have the thing working.
- (void)applicationDidFinishLaunching:(NSNotification*)aNotification {
/* GViewController subview of NSViewController */
GViewController *g = [[GViewController alloc] initWithNibName:#"GViewController" bundle:nil];
[contentView addSubview: g];
}
Those methods have to be present in a subclass of NSView, not NSViewController. It also doesn't make any sense to do addSubview:someViewController; the argument to that method needs to be a view.
override func keyDown(with event: NSEvent) {
super.keyDown(with: event)
Swift.print("Caught a key down: \(event.keyCode)!")
}
override keyDown with event worked for me in swift
you can try same method in object-c.
check out offical doc
https://developer.apple.com/documentation/appkit/nsresponder/1525805-keydown?language=objc