ios - 如何在每次添加时将文本显示为 UIView 的数量作为 ULabel

标签 ios objective-c uiview

目前我创建了一个可拖动的 UIView 作为 subview ,当前在每次创建时显示其中的文本。但是,我希望其中的文本随着每次创建而改变,例如第一个 UIView 显示 1,第二个显示 2,第三个显示 3,等等。到目前为止,这是我的代码:

- (void)panWasRecognized:(UIPanGestureRecognizer *)panner {

{

UIView *draggedView = panner.view;

CGPoint offset = [panner translationInView:draggedView.superview];
CGPoint center = draggedView.center;
draggedView.center = CGPointMake(center.x + offset.x, center.y + offset.y);
draggedView.layer.borderColor = [UIColor blueColor].CGColor;
draggedView.layer.borderWidth = 4.0f;


// Reset translation to zero so on the next `panWasRecognized:` message, the
// translation will just be the additional movement of the touch since now.
[panner setTranslation:CGPointZero inView:draggedView.superview];

}




}



- (IBAction)addRepButton:(UIBarButtonItem *)newRep {

buttonCount ++;
if (buttonCount > 1 )
{



    UILabel *textField = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
    textField.userInteractionEnabled = YES;
    textField.layer.cornerRadius = 20;
    [textField setBackgroundColor: [UIColor whiteColor]];
    textField.font = [UIFont systemFontOfSize:20];
    textField.layer.borderColor = [UIColor blackColor].CGColor;
    textField.textColor = [UIColor blackColor];
    textField.layer.borderWidth = 4.0f;
    textField.text = @"1";
    textField.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:textField];







UIPanGestureRecognizer *panner = [[UIPanGestureRecognizer alloc]
                                  initWithTarget:self action:@selector(panWasRecognized:)];
[textField addGestureRecognizer:panner];




}


}
@end

最佳答案

您需要的是该类的一个属性,用于记录标签的数量。所以在接口(interface)(头文件,或者你可以在主文件的顶部添加一个接口(interface))添加一个整数属性,就像这样:

@property (nonatomic, assign) NSInteger labelCounter;

在您的 viewDidLoad 方法中将计数器初始化为零,例如:

self.labelCounter = 0;

现在在您的 addRepButton 方法中,将标签文本设置为该数字的值,如下所示:

textField.text = [NSString stringWithFormat:@"%i", self.labelCounter];

并且在相同的方法中,增加下一个标签的计数器:

self.labelCounter++;

应该可以了。

关于ios - 如何在每次添加时将文本显示为 UIView 的数量作为 ULabel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33294824/

相关文章:

objective-c - cocoa webView -elementAtPoint : (OS X) 异常崩溃

iphone - 我们可以为 UIView 的 transitionWithView :duration:options:animations:completion: method? 传递多个选项吗

iOS:添加到 View 触发器的图层(在 CADisplayLink 上更新)[view layoutSubviews] 60x/sec - 为什么?

android - 推送通知GCM/APNS组 "big view"仅显示5条消息(然后显示xx条新消息)

objective-c - 解析套件构建项目问题

ios - 存折应用程序未在 iOS 模拟器中更新?

ios - UIView 响应动画后的方向变化

ios - 如何快速使用来自 reverseGeocodeLocation 的数据?

ios - 更改后我的框架高度没有更新

objective-c - 如何连接到无线网络