Get Index of Object in TouchesEnded - objective-c

i have a UIViewController with many UIImageViews. I need in touchEnded event get a index of this UIimage.
How do Make?
Thank's

make a custom UIImageview class which is subclass of UIControl. and add a target method for this class.set the tag for each UIImageview. and change the class name of UIImageview in inspector window to that custom class.
[imageview addTarget: self action:#selector(findIndex:)forControlEvents:UIControlEventTouchUpInside ]
you can set this method using xib also.
and define findIndex method in your viewController class.

Instead of UIImageViews, why don't you use UIButtons? You can set the image with setImage:forState:, and the target action and object with addTarget:action:forControlEvents. When your target action gets called, the button will be sent as the sender argument.

Related

Changing UILabel text from UICollectionView custom delegate class

In my storyboard I have a UICollectionView, with cells, sections, etc. I set as delegate and datasource a custom class (so, not the classic myCollectionView.delegate = self) but a custom class that cares about the retrieving of datas.
In this UICollectionView I have a label that I need to update with a certain value that I handle in the delegate class. (It's a property of the selected cell).
How can this be done? I read about KVO and Notification, but i'm not sure what I have to do.
More Detail
MatchCollectionViewController
In this one:
viewDidLoad
A gameView that act as a container for the following collectionView
CollectionView declaration (once created the collectionView i add it to the gameView)
The delegate of the CollectionView is the following class CollectionViewUiGrid
CollectionViewUiGrid
In this class i implement all the delegate and dataSource methods for the collectionView
initWithFrame (and initWithFrame with my own parameters)
in the method cellForItemAtIndexPath i return an istance of a class (UiGameCell) that extends UiCollectionViewCell
In the method didSelectItemAtIndexPath i have this logic.
If a cell is selected, assign retrieve his property, called CellDescription).
This is the property that i would like to be as my UiLabel value in MatchCollectionViewController.
I found a solution.
As mentioned in
Pass object with NSNotificationCenter to other view
Probably one of the simplest way is to use a NsNotificationCenter.

How to bind click action of NSButton in view based NSTableView

I have an NSTableView that is set to be 'view based', and within each NSTableCellView there is an NSButton and an NSTextField.
The text field is being populated correctly from an array controller. The buttons are appearing correctly but I'm having trouble working out how to hook up the click action.
I thought this would be possible by control-dragging from the NSButton in IB to a simple method like this one in my controller (in this case an NSDocument subclass):
- (IBAction)testAction:(NSButton *)sender {
NSLog(#"Test action");
}
It connects fine but never gets fired. Any ideas why this is or how to fix it?
I don't understand why this works, but I had the same problem and was able to get it working by assigning the table delegate and datasource to the file owner within IB, which is also the class of my click handlers. Only then did it seem to actually bind the click handlers for the buttons in my cell view. Previously I was setting the delegate and datasource in code after the view was loaded.
You have to subclass NSTableCellView class. put your onClick Action method in subclass files.
Let me know if i am not clear..

How should I implement [almost] identical actions used throughout various VCs? (Answer: use a category)

Right now I am using the delegate, but I suspect it's bad form.
I programatically create an identical UIBarButtonItem in numerous, different view controllers.
My button's target is myAppDelegate, and its action is defined therein.
What is a better way of doing this? Or am I just supposed to copy and paste the identical action into no-matter-how-many view controllers that instantiate the identical bar button?
Okay, now suppose the action is identical in all respects but one: It varies only in that it should send a presentViewController message to the view controller that instantiated the button that sent the action. Thus, in the action, I can send a presentViewController message to sender, which is an instance of the button, but I know no means of reaching the view controller that instantiated that instance of the button.
Each view controller simply needs to set itself as the target of the button when the button is created. The button's action selector will then be sent to the correct controller.
As for the selector itself, create a category on UIViewController to contain the button's action:
#implemenation UIViewController (MKCBarButtonItemAction)
- (void)MKCDoThatThingIDo
{
// Do things.
[self presentViewController:[self theOtherViewControllerWhatINeedToDisplay]
animated:YES
completion:^{
// Do more things.
}];
// Do yet other things.
}
#end
If you've common methods for multiple view controllers defined in AppDelegate file, then you can make call to those actions using delegate object something like,
#define appDelegateObj (AppDelegate *)[UIApplication sharedApplication].delegate;
Where, AppDelegate is your delegate file class name, also define this macro at global so you can access it from anywhere,
Now where ever you want to call any function, just use like [appDelegateObj functionName];
Here's one thing, if you can make class methods into delegate file then, it can used with using class name, like [AppDelegate functionName]; 
class methods defined like +(void)functionName{...} and it can't access to class instance variables and methods.

How do I have a subclassed UIGestureRecognizer class call a selector on a view

I have a view called GameViewController. I didn't set it up but it seems to be a UIKit view with an embedded EAGL view.
I set up gesture recognizers for tap and swipe on my GameViewController. They work great, except the method I set up as a selector for tap (a function within GameViewController) gets called on touchesEnded, not touchesBegan.
Only way to get at the touchesBegan function is to subclass.
So, I subclassed UITapGestureRecognizer, created a touchesBegan function, and NSLogs in there get called on touch down. However, I can't call any functions in GameViewController from the UITapGestureRecognizer subclass. (Class method +METHODIWANTTOCALL not found).
I realize I need "references" back and forth but what should they be? Or is this totally the wrong approach? Do I delegate? (I'm new to that) What is the best way to have a method within GameViewController called from this UITapGesture subclass?
It seems like your GameViewController is a singleton class (only one instance of this class in the app). You can have a class method in GameViewController that returns the instance of the singleton. Then you can just the method regularly:
In Recognizer:
GameViewController* myController = [GameViewController getInstance];
[myController handleTouchBegin];
Alternatively, you can define your handleTouchBegin as a class method if it does not need to know any internal state:
[GameViewController handleTouchBegin];

Loading Accessory callout view for mkannotationview

I have a map annotation view that contains a rightcallout button which loads an accessory view which is a UIViewController class. I am using resuable annotations, but am wondering how I can pass updated information to my UIViewController class. Let's say I have 2 string values which map to 2 UILabels on my view. How can I update those values after the initial accessory view has already been loaded into memory as a resusable view?
Any help would be appreciated.
You'll need to maintain a reference to the UILabels in the object that gets the update, and then use setTitle: (I think) to update the labels.
In your annotation subclass you need to override the setTitle method to send the changes to the instance of your UIViewController class that your subclass is holding. Or, you could setup your annotation subclass to receive notifications (from NSNotificationCenter), and upon receiving a notification, update the title and the instance of your UIViewController class.
If you are unfamiliar with NSNotifications, then here is a quick reference. I used these to keep my annotations updated.
NSNotification Example
Try using the MKMapViewDelegate method:
-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control;
This method is called when a user tapped one of the annotation view’s accessory buttons. Assuming that your MKMapViewDelegate is also the UIViewController that can access your accessory view.