我的 Storyboard中有一个标签,我正在 viewDidLoad
中为我的标签设置文本,但是当我调用我的方法时,标签没有更新。当我的 scrollview 滚动时,我正在调用我的 method1。
这是我的代码示例;
//.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;
}
我应该如何更新我的标签?谢谢。
最佳答案
您需要使用 Xcode 中的 Counterparts 拆分 Pane View 将您的标签对象按住控制键并拖动到您的实现 (viewControllerClass.m) 文件,并且您应该给它一个有意义的名称(“标签”本身就很模糊)。
然后您可以使用以下方式设置文本:
someLabel.text = @"some text";
然后您可以尝试使用已声明的 NSString 变量来设置文本:
NSString *labelText = @"some text";
someLabel.text = labelText;
如果这不起作用,请返回 IB 并打开“实用工具”面板。显示连接检查器(最右边)以确认它具有正确的引用 socket (someLabel - viewControllerClass)。您可能不小心将其连接了两次。
关于ios - 在方法 iOS 中更新标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21469735/