我有一个UIView
是menuiitems,这些菜单项是动态创建的,我在每个uiview标签上加1,但是当我想在用户点击菜单项时检索uiview标签值我得到一个不同的标签号。
我想检索存储了id的标记,以便我可以将该id发布到php脚本。
我在设置标签时如何检索标签?
- (void)handleSingleTap:(UITapGestureRecognizer *)recognizer {
UIView *tempView = recognizer.view;
NSNumber *tag = [NSNumber numberWithInt:tempView.tag];
int idCat = [tag intValue];
NSLog(@"TAG %d", 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];
}
这是在另一个视图中创建菜单项的代码:
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString: @"http://localhost:8888/MAMP/WHFC/categories.php"]];
int i = 0;
NSError *e;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&e];
NSArray *arrCategoryList = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&e];
for(NSDictionary *dictCategory in arrCategoryList)
{
NSString *strCategoryId = [dictCategory objectForKey:@"CategoryId"];
NSString *strCategoryName = [dictCategory objectForKey:@"Name"];
NSLog(@"%@ : %@",strCategoryId,strCategoryName);
UIView *linkMenu = [[UIView alloc] init];
[linkMenu setFrame:CGRectMake(10, i+1, 300, 35)];
linkMenu.tag = [NSString stringWithFormat:@"%d", strCategoryId];
linkMenu.layer.zPosition = 1;
[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;
}
答案 0 :(得分:0)
如果你有一个UIView
让我们说放在里面的所有其他菜单项也是UIViews
让我们说B,C和D那么很可能是识别器。你回来了A无论B,C或D中的哪一个被按下。
将此内容写入handleSingleTap
函数:
CGPoint touchPoint=[gesture locationInView:scrollView];
for(UIView * subView in myView ) //here myView should be the superView A where you have added all the menu item view B, C, and D
{
if(CGRectContainsPoint(subView.bounds, touchPoint))
{
NSNumber *tag = [NSNumber numberWithInt:subView.tag];
}
}
该标记变量将包含您需要的最终标记。