Objective-C NSArray 对象到 UiLabel setText(在循环中)
我有一个启用分页的 UIScrollView 。 对于 Scroll 中的每个页面,我想更改 UILabel 中的文本。 例如。 30 个 NSArray 对象=30 页。
我在没有 NSArray 的情况下也能正常工作,但我确实需要将 NSArray 对象加载到页面。
<代码> //view did load...所有滚动视图属性和其他废话
CGRect pageFrame ;
NSArray *arr = [NSArray arrayWithObjects: @"A", @"B", @"C", nil];
for (int i = 0 ; i < kNumberOfPages ; i++)
{
pageFrame = CGRectMake(i * scrollView.bounds.size.width, 0.0f, scrollView.bounds.size.width, scrollView.bounds.size.height) ;
labelOne = [[UILabel alloc] initWithFrame: pageFrame] ;
[scrollView addSubview: labelOne] ;
[labelOne release] ;
[labelOne setFont: [UIFont boldSystemFontOfSize: 140.0f]] ;
[labelOne setTextAlignment: UITextAlignmentCenter] ;
[labelOne setTextColor: [UIColor darkTextColor]] ;
[labelOne setBackgroundColor:[UIColor clearColor]];
scrollView.backgroundColor = [UIColor clearColor];
for (NSString *new in arr){
[labelOne setText:new];
NSLog(new);
}
}
[scrollView release];
并且它抛出异常。 有什么想法吗? 基本上,我需要将数组中的每个对象获取到 setText(NSString)。
谢谢!
I have a UIScrollView with enabled paging.
For every page in Scroll i want to change the text in UILabel.
Eg. 30 NSArray objects=30 pages.
I got it working without NSArray, but i really need to load NSArray object(s) to the page.
//view did load...All that scroll view properties and other crap
CGRect pageFrame ;
NSArray *arr = [NSArray arrayWithObjects: @"A", @"B", @"C", nil];
for (int i = 0 ; i < kNumberOfPages ; i++)
{
pageFrame = CGRectMake(i * scrollView.bounds.size.width, 0.0f, scrollView.bounds.size.width, scrollView.bounds.size.height) ;
labelOne = [[UILabel alloc] initWithFrame: pageFrame] ;
[scrollView addSubview: labelOne] ;
[labelOne release] ;
[labelOne setFont: [UIFont boldSystemFontOfSize: 140.0f]] ;
[labelOne setTextAlignment: UITextAlignmentCenter] ;
[labelOne setTextColor: [UIColor darkTextColor]] ;
[labelOne setBackgroundColor:[UIColor clearColor]];
scrollView.backgroundColor = [UIColor clearColor];
for (NSString *new in arr){
[labelOne setText:new];
NSLog(new);
}
}
[scrollView release];
And it throws an exception.
Any ideas?
Basically, i need to get every object from my array to setText(NSString).
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
编辑(因为我最初读错了问题......):
两个问题。正如@Eiko所说,您需要删除
[scrollView release];
行。然后,问题是您不断循环数组并用下一个字符串替换文本,这最终只会将标签文本设置为最后一个字符串。您只需使用数组中相应位置的一个字符串。
而不是:
使用这个:
EDIT (because I misread the question originally...):
Two problems. As @Eiko says, you need to remove the
[scrollView release];
line.Then, the problem is that you keep looping through your array and replacing the text with the next string, which will end up only setting the label text to your last string. You need to use just the one string in the corresponding position in the array.
Instead of:
Use this: