gpt4 book ai didi

iphone - 多个 UILabel 实例

转载 作者:行者123 更新时间:2023-11-29 13:05:47 24 4
gpt4 key购买 nike

我正在创建 UILabel 的多个实例以显示在 map 上,该 map 将具有许多标签并允许用户返回所选标签的 ID 或标签号。下面的代码只返回最后创建的实例,可能是由于相同的变量“newCity”。

这两个实例将从 pList 中动态填充数据,我将循环遍历该 pList 以获取标签的城市、idNumber 和 x & y 位置。城市的数量每次都会不同,甚至可能会动态变化。为清楚起见,显示了两个。

问题是如何为被点击的正确标签获取正确的 idNumber?我已经搜索了很多地方,但没有发现在不引起其他问题的情况下有效的解决方案。如何创建新实例并仍然获得正确的实例?

另请注意,下面的 tapGestureRecognizer 具有 tapGesture 和 tapGesture2 名称。它不适用于同一个。如何使用具有一个名称的 tapGesture 并识别哪个标签?

我宁愿使用标签而不是按钮,除非别无他法。我什至可以添加一个 touchesBegan 或 touchesMoved 来调整我不想与标签中的 tapGesture 冲突的 subview 。

感谢您的帮助。

WBCitiesView.h

@interface WBCitiesView : UIViewController 
{
UIView *subView;
int resultNumber;
}

@property(nonatomic, strong) UIView *subView;
@property int resultNumber;

- (void) touchUp:(id)sender;

@end

WBCitiesView.m

#import "WBCitiesView.h"
#import "WBCities.h"


@interface WBCitiesView ()

@end

@implementation WBCitiesView

@synthesize subView, resultNumber;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {

}
return self;
}


WBCities *newCity;


- (void)viewDidLoad
{
[super viewDidLoad];

CGRect mapFrame = CGRectMake(20, 30, 250, 300);
subView = [[UIView alloc]initWithFrame:mapFrame];
subView.backgroundColor = [UIColor grayColor];
subView.alpha = .5;
subView.autoresizesSubviews=YES;
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:subView action:@selector(swipe:)];///was self
[subView addGestureRecognizer:swipe];
[self.view addSubview:subView];




newCity =[[WBCities alloc] initWithFrame:CGRectMake(50, 20, 100, 30)];
newCity.city = @"CityA";
newCity.idNumber = 1111;
newCity.size = 0;
[newCity setBackgroundColor:[UIColor blueColor]];
[newCity setText:newCity.city];
[newCity setTag:newCity.idNumber];
newCity.userInteractionEnabled = YES;
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(touchUp:)];
[newCity addGestureRecognizer:tapGesture];
[subView addSubview:newCity];


newCity =[[WBCities alloc] initWithFrame:CGRectMake(100, 70, 100, 30)];
newCity.city = @"CityB";
newCity.idNumber = 2222;
newCity.size = 1;
[newCity setBackgroundColor:[UIColor yellowColor]];
[newCity setText:newCity.city];
[newCity setTag:newCity.idNumber];
newCity.userInteractionEnabled = YES;
UITapGestureRecognizer *tapGesture2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(touchUp:)];
[newCity addGestureRecognizer:tapGesture2];
[subView addSubview:newCity];



-(void) touchUp:(id)sender
{

resultNumber = [newCity returnIdNumber];
}

WBCities.h

@interface WBCities : UILabel{
NSString *city;
int idNumber;
BOOL size;
}

@property (nonatomic, strong) NSString *city;
@property (nonatomic) int idNumber;
@property (nonatomic) BOOL size;

-(int)returnIdNumber;

@end

WBCities.m

#import "WBCities.h"
#import "WBCitiesView.h"

@implementation WBCities

@synthesize city, idNumber, size;

-(id)init{
self = [super init];
if (self){

}
return self;
}

-(int)returnIdNumber{

return idNumber;
}

@end

最佳答案

用下面的代码替换你的-(void) touchUp:(id)sender方法

-(void) touchUp:(id)sender
{
UITapGestureRecognizer *tapGesture = (UITapGestureRecognizer*)sender;
WBCities *cityLabel = (WBCities*)tapGesture.view;
resultNumber = [cityLabel returnIdNumber];
}

关于iphone - 多个 UILabel 实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18690023/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com