0

I need to write some gui for IOS, yet i am so used to the automatic view id's that android has, that i am at a loss when it comes to identifying views without putting explicit tags in the interface builder

is there some way to automatically identify iOS views from interface builder or otherwise ?

maybe a library that does that ?

Lena Bru
  • 13,521
  • 11
  • 61
  • 126
  • Create your own custom property here is a link that you can refer:- http://stackoverflow.com/questions/5500327/subclass-uibutton-to-add-a-property – Leena Jan 16 '14 at 07:58
  • not exactly what i mean, the same way i dont need a category, i can set the tag programmatically -> i am looking for something that will do that AUTOMATICALLY – Lena Bru Jan 16 '14 at 08:01
  • I am not asking you to create category for that i am just asking you to create custom property for your view in a class where you need. – Leena Jan 16 '14 at 08:02
  • Can id be just objective C id of object (id - pointer) – Leszek Zarna Jan 16 '14 at 08:14

2 Answers2

1

Thats why you have IBOutlets. Create an IBOutlet for your views, like so IBOutlet UIView *view1; etc etc then in link the IBOutlet to your view in Interface Builder. Now you can progammatically use the view1 variable which will modify your view associated to that IBOutlet in interface builder\

UPDATE

Progammatically create all your views like so:

for(int i = 0; i < 20; i++){
    //set your x and y coordinates with some awesome maths, maybe you want to create a grid, so update the x coordinate accordingly
    UIView *view = [UIView alloc] initWithFrame:CGRectMake(whateverframe)];
    UITapGestureRecognizer *singleTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewTapped:)];
    [imageView addGestureRecognizer:singleTapGesture];

    [singleTapGesture release]; //remove line if you have an ARC project

    view.tag = i; //this is just for you since you like to handle your views with id tags
}

Then your one method that will handle the taps for all your code

- (void) viewTapped:(UIGestureRecognizer*)recognizer
{
     UIView *viewTapped = recognizer.view; // This is the view associated with gesture recognizer.
    int id = viewTapped.tag; //heres the tag id do whatever you like

    //do something with either that view variable which will return the view that was tapped
    //maybe you wanted to change the color of it or something or change the contents of it

    //or you can do something with the id tag of that view.

    //or maybe you just want to handle it with if else statements like so
    //if(viewTapped.tag == 1) //do this
    //elseif(viewTapped.tag == 2) // etc etc
}
Pavan
  • 17,840
  • 8
  • 59
  • 100
  • not exactly what i want-> i have a screen with 20 clickable views on it, i want to have 1 method that governs the clicks on that screen, not 20, so i need to differentiate between the given views, and except for putting tags and defining constants for those tags, i don't see any other solution – Lena Bru Jan 16 '14 at 07:52
  • @LenaBru Check the updated code. Sure, then you can do all this progammatically then instead. One method to handle all the taps for your UIViews. – Pavan Jan 16 '14 at 08:05
0

Set Tag for each and every view using property setTag and retrieve the corresponding view using – viewWithTag:. [abaseView subViews] returns an array of subviews. Filter the subviews using Class and Tag.

Muruganandham K
  • 5,271
  • 5
  • 34
  • 62
  • viewWithTag can be very costly, as it traverses the entire view hierarchy for the desired view on every call – Lena Bru Jan 16 '14 at 08:18