【问题标题】:Update label in method iOS在 iOS 方法中更新标签
【发布时间】:2014-01-30 23:33:50
【问题描述】:

我的故事板中有一个标签,我正在为viewDidLoad 中的标签设置文本,但是当我调用我的方法时,标签没有更新。当我的滚动视图滚动时,我正在调用我的方法 1。

这是我的代码示例;

//.h
NSString *text;

//.m

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{

[self method1];    

}
-(void) method1{

text = @"Here is the text";

NSRunLoop *runloop = [NSRunLoop currentRunLoop];
NSTimer *timer = [NSTimer timerWithTimeInterval:0.1 target:self selector:@selector(updateLabel) userInfo:nil repeats:YES];
[runloop addTimer:timer forMode:NSRunLoopCommonModes];
[runloop addTimer:timer forMode:UITrackingRunLoopMode];

}

-(void) updateLabel{

label.text = text;

}

我应该如何更新我的标签?谢谢。

【问题讨论】:

  • 您是否将标签连接到视图控制器中的IBOutlet
  • 你执行过updateLabel方法吗?
  • is text an ivar.... 我在 -(void) changeDiscount:(int)currentPagePresentation{ 和 label.text = text 中看到了这个 text = @"a";;用不同的方法
  • 设置断点并确保文本在进入该方法时确实是a。当然要确保它不为空
  • @user3255346 将设置它,但您仍然需要在 Interface Builder 中连接它

标签: ios objective-c uilabel


【解决方案1】:

您需要使用 Xcode 中的 Counterparts 拆分窗格视图控制并拖动您的标签对象到您的实现 (viewControllerClass.m) 文件,并且您应该给它一个有意义的名称(“标签”单独是相当模糊的)。

然后您可以使用以下方式设置文本:

someLabel.text = @"some text";

然后你可以尝试使用声明的 NSString 变量来设置文本:

NSString *labelText = @"some text";
someLabel.text = labelText;

如果这不起作用,请返回 IB 并打开实用程序窗格。显示连接检查器(最右侧)以确认它具有正确的引用插座(someLabel - viewControllerClass)。你可能不小心把它连线了两次。

【讨论】:

  • 我查看了inspector,Outlets部分只有一个标签。
  • 这对你有用吗? someLabel.text = @"一些文字";
猜你喜欢
  • 2021-06-25
  • 2019-06-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多