gpt4 book ai didi

ios - 从 iOS 应用程序执行 PHP 文件并刷新标签文本

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:07:14 27 4
gpt4 key购买 nike

在我的应用程序中,我需要包含一个按钮操作来更新远程服务器中 MySQL 字段的值。当用户点击按钮时,应用应将字段值加 1。

MySQL 值显示在 UILabel 中。

我已经编写了 PHP 文件来更新数据库。

在我的 viewController 中,我获取要传递给 PHP 文件的 id 字段。

这是执行值更新的按钮操作:

- (IBAction)votarAction:(id)sender {
//URL definition where php file is hosted
int categoriaID = [[detalleDescription objectForKey:@"idEmpresa"] intValue];

NSString *string = [NSString stringWithFormat:@"%d", categoriaID];
NSLog(@"ID EMPRESA %@",string);
NSMutableString *ms = [[NSMutableString alloc] initWithString:@"http://mujercanariasigloxxi.appgestion.eu/app_php_files/cambiarvaloracionempresa.php?id="];
[ms appendString:string];
// URL request
NSLog(@"URL = %@",ms);
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:ms]];
//URL connection to the internet
[[NSURLConnection alloc]initWithRequest:request delegate:self];

}

我需要的是在点击按钮后更新字段值,以在 UILabel 上显示新值。哪种方法最好?

最佳答案

这是一个很好的方法:

dispatch_queue_t backgroundQueue = dispatch_queue_create("com.domai.queue", 0);

dispatch_async(backgroundQueue, ^{

int categoriaID = [[detalleDescription objectForKey:@"idEmpresa"] intValue];

NSString *string = [NSString stringWithFormat:@"%d", categoriaID];
NSLog(@"ID EMPRESA %@",string);
NSMutableString *ms = [[NSMutableString alloc]
initWithString:@"http://url.php?id="];
[ms appendString:string];

// URL request
NSLog(@"URL = %@",ms);
NSURLRequest *request =
[NSURLRequest requestWithURL:[NSURL URLWithString:ms]];

//URL connection to the internet
NSData *responseData = [[NSURLConnection alloc]
initWithRequest:request delegate:self];
NSLog(@"responseData= %@",responseData);//["34"]

//Parse JSON
NSMutableDictionary * json = [NSJSONSerialization
JSONObjectWithData:responseData
options: NSJSONReadingMutableContainers
error: &error];

NSString *labelValue = (NSString*) [json objectAtIndex:0];

dispatch_async(dispatch_get_main_queue(), ^{
//update your UIlabel dataLabel
dataLabel.text = labelValue;
});
});

您应该在后台线程中发出请求并返回 JSON,然后在主队列中更新 UILabel。

关于ios - 从 iOS 应用程序执行 PHP 文件并刷新标签文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22109591/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com