我想要做的是我从php脚本中获取一个数字。此数字确定必须添加到视图的标签。 php脚本非常简单,如下所示:
<?php
echo (int)5;
?>
objc脚本必须确定数字,并且需要编写php脚本描述的许多标签。我在另一个标签中显示字符串以确保返回该号码。如何将这些标签写入我的视图?
我得到了一些代码如下:
- (void)viewDidLoad
{
NSString *post = @"";
NSData *postData = [post dataUsingEncoding:NSISOLatin1StringEncoding allowLossyConversion:NO];
NSMutableURLRequest *urlRequest = [[[NSMutableURLRequest alloc] init] autorelease];
[urlRequest setURL:[NSURL URLWithString:@"http://......./testGet.php"]];
[urlRequest setHTTPMethod:@"POST"];
[urlRequest setHTTPBody:postData];
NSData *urlData;
NSURLResponse *response;
NSError *error;
urlData = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:&error];
if(!urlData) {
NSLog(@"No connection!");
}
NSString *aStr = [[[NSString alloc] initWithData:urlData encoding:NSUTF8StringEncoding]autorelease];
int i;
i = [aStr intValue];
status.text = [NSString stringWithFormat:@"%d",i];
for (int i; i <= 10; i++)
{
UILabel *mijnlabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 100, 200, 100)];
mijnlabel.text = @"Loremipsum";
[self.view addSubview:mijnlabel];
}
[super viewDidLoad];
}
答案 0 :(得分:2)
虽然您的标签将全部显示在同一位置,但您可以通过向视图发送[self.view addSubview:mijnlabel];
来执行此操作。
你必须小心这些事情:
UILabel
。viewDidLoad
到超级视图。所以你的结果可能是:
// first
[super viewDidLoad];
...
NSString *aStr = [[[NSString alloc]
initWithData:urlData
encoding:NSUTF8StringEncoding]autorelease];
int labelsCount = [aStr intValue];
status.text = [NSString stringWithFormat:@"%d",labelsCounts];
// another way, in order to display the label under each other
// this will display labelsCount labels
int i;
for(i = 0; i < labelsCount; i++) {
CGFloat yCoord = i * 100;
UILabel *mijnlabel = [[UILabel alloc]
initWithFrame:CGRectMake(50, yCoord, 200, 100)];
mijnlabel.text = @"Loremipsum";
[self.view addSubview:mijnlabel];
[mijnLabel release];
}