redoc01
redoc01

Reputation: 2325

Custom UIView and setting and getting custom value back from variable inside UIView

I have a custom uiview where i have a setter and a getter when the uiview is dynamically created i set this value like this:

for(NSDictionary *dictCategory in arrCategoryList)
{
    NSString *strCategoryId = [dictCategory objectForKey:@"CategoryId"];
    NSString *strCategoryName = [dictCategory objectForKey:@"Name"];
    NSLog(@"%@ : %@",strCategoryId,strCategoryName);

    UIViewMenuItem *linkMenu = [[UIViewMenuItem alloc] init];
    [linkMenu setFrame:CGRectMake(10, i+1, 300, 35)];
    [linkMenu setId:strCategoryId]; //here i set the value in the custom uiview
    linkMenu.layer.zPosition = 7;
    [viewSlide3 addSubview:linkMenu];
    [linkMenu setBackgroundColor:[UIColor blueColor]];
    linkMenu.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:.9];

    UITapGestureRecognizer *singleFingerTap =
    [[UITapGestureRecognizer alloc] initWithTarget:self
                                            action:@selector(handleSingleTap:)];
    [linkMenu addGestureRecognizer:singleFingerTap];

    UILabel *labelMenu = [[UILabel alloc] init];
    [labelMenu setFrame:CGRectMake(20, 0, 300, 35)];
    [labelMenu setFont:[UIFont systemFontOfSize:16]];
    [labelMenu setTextColor:[UIColor whiteColor]];
    [linkMenu addSubview:labelMenu];
    [labelMenu setText:strCategoryName];




    i = i + 35 + 1;
}

Now when i tap on the custom uiview i want to get back the value from the custom uiview so I'm doing this:

- (void)handleSingleTap:(UITapGestureRecognizer *)recognizer {
CGPoint location = [recognizer locationInView:[recognizer.view superview]];

CGPoint touchPoint=[recognizer locationInView:[recognizer.view superview]];



UIViewMenuItem *tempView = (UIViewMenuItem *)recognizer.view;
NSNumber *tag = [NSNumber numberWithInt:tempView.tag];
NSString *idCat = [tempView getCatId];

NSLog(@"TAG %@",idCat);

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString: [NSString stringWithFormat:@"http://localhost:8888/MAMP/WHFC/SubCategories.php?categoryid=%d", idCat]]];

int i = 0;

NSError *e;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&e];
NSArray *arrCategoryList = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&e];




UIViewController *viewController = [[UIViewController alloc] init];
viewController.view.backgroundColor = [UIColor whiteColor];

UIView *uiView = [[UIView alloc] init];
[uiView setFrame:CGRectMake(0, 480, 320, 480)];
[uiView setBackgroundColor:[UIColor grayColor]];

viewController.view = uiView;


UITableView *uiTableView = [[UITableView alloc] init];
[uiTableView setFrame:CGRectMake(0, 0, 320, 480)];

[uiView addSubview:uiTableView];

[self presentViewController:viewController animated:YES completion:nil];



//Do stuff here...

}

But i keep get the same value "13" from NSString *idCat = [tempView getCatId];

This is the custom UIView class:

#import <UIKit/UIKit.h>

@interface UIViewMenuItem : UIView
- (void) setId: (NSString *) cId;
- (NSString *) getCatId;


@end
NSString *catId;


#import "UIViewMenuItem.h"

@implementation UIViewMenuItem


- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
    // Initialization code
}
return self;
}

- (void) setId: (NSString *) cId;
{
catId = cId;
//If possible, set things up for the new word
}

- (NSString *) getCatId{


return catId;


}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/

@end

Upvotes: 0

Views: 518

Answers (1)

Hermann Klecker
Hermann Klecker

Reputation: 14068

There is nothing wrong, in principle, with using a hidden (not visible from outside the class) instance variable cId and providing getters and setters.

But yours isn't an instance variable. It is declared somewhere between the interface and the implementation. It's a global variable or static. (I am not 100% positive about the global, but I think it is. I ran into a linker issue - dublicate object - once when I did the same mistake and did it in two classes using the same variable name. However, it is not that important whether it is global or just static. Being static is bad enough.)

This means at least that all the instances of UIViewMenuItem share the same variable (!!!).

First: Move it to somewhere between @implementation and its @end. Then it should work as expected.

Then: Be lazy and solve it the objective-c-way. Get rid of the variable and get rid of the getter and setter. If you are happy with the variable being public (it's accessible though getter and setter anyway) then just add a @property (nonatomic, retain) NSString *cid; to the interface. Unless you've got an older compiler then that is basically it. The compiler will add a getter and a setter (getCId and cId) automatically.

The compiler will add an iVar _cId that you would not use yourself in most cases. I am not 100% positive whether the name of the iVar will be cId or _cId when you use the comfortable way. If you care then you can conrol the name of the iVar by adding an @synthesize statment to the implementation and define the iVar name as you like. You could add much more customization (again declaring the iVar yourself, providing custom getters and setters) but there is no need for that when all you need from the property is what you have shown in your question and its code examples.

Upvotes: 1

Related Questions