热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

在iPhone上运行另一个进程时管理视图-ManagingaviewwhileanotherprocessrunsoniPhone

Iamhavingabitofaproblemhere,IamlettheuseruploadsomevideototheserverhoweverIam

I am having a bit of a problem here, I am let the user upload some video to the server however I am having some difficulties managing a view that I am using to illustrate its progress, I know why the problem is happening and i found a way around it (sort of) here my problem

我在这里遇到了一些问题,我让用户将一些视频上传到服务器但是我在管理我用来说明其进展的视图时遇到一些困难,我知道为什么问题发生了,我发现了绕过它的方式(有点)我的问题

So if one tries to make some code that looks something like this (in a UIViewController

所以,如果有人试图制作一些看起来像这样的代码(在UIViewController中)

-(void)uploadMovie
{
   UIActivityView indicator=new...
  [self.view addSubview:indicator]
   [uploader UploadMyMovie:data]

 }

This code wont work, the uploader will lock the controller and will not allow time for the indicator to come on screen in time, i found waiting for a few seconds before calling the uploader works but i took another approach.

这段代码不起作用,上传器将锁定控制器并且不会让指示器及时进入屏幕的时间,我发现在调用上传器工作之前等了几秒钟,但我采取了另一种方法。

The approach was to start a new thread for the uploader and have a protocol in which the uploader object informs the UIViewController (or some delegate) when it starts uploading, its progress, and when it finishes uploading. Something like

方法是为上传器启动一个新线程,并有一个协议,上传者对象在开始上传,进度以及完成上传时通知UIViewController(或某个委托)。就像是

     -(void)uploadMovie
    {
       UIActivityView indicator=new...
      [self.view addSubview:indicator]
       NSThread *thread=...
       [thread start]
     }

the delegate methods look something like this

委托方法看起来像这样

    #pragma mark UploadProgressDelegate

-(void)didStartUploading
{

    progressLabel= [[UILabel alloc] initWithFrame:CGRectMake(93, 240, 116, 32)];
    ind= [[UIActivityIndicatorView alloc]   initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    ind.center=self.view.center;
    [progressLabel setTextColor:[UIColor blackColor]];
    [progressLabel setText:@"TEST"];
    [self.view addSubview:progressLabel];
    [self.view addSubview:ind];
    [ind startAnimating];
    [self.view bringSubviewToFront:progressLabel];
    [ind setHidesWhenStopped:TRUE]; 
    [self.view setUserInteractionEnabled:FALSE];
}
-(void)progressUpdate:(NSString*)progress
{
     [progressLabel setText:progress];
}
-(void)didEndUploading;
{
    [progressLabel removeFromSuperview];
    [ind stopAnimating];
    [ind removeFromSuperview];
    [progressLabel release];
    [ind release];
    [self.view setUserInteractionEnabled:TRUE];
}

This works great and the indicator shows and everything, then i decided to let the user see the progress by adding a UILabel (reflected in the code above), howeverr for this the solution does not work, the label does not display and ofcourse no updates...

这很好用,指示器显示和所有内容,然后我决定让用户通过添加UILabel(反映在上面的代码中)看到进度,但是对于此解决方案不起作用,标签不显示且当然没有更新...

I was wondering if anyone has encounter this situation and has come up with a solution for it? or if you can possibly see from the code above why the label isnt showing...

我想知道是否有人遇到过这种情况并提出了解决方案吗?或者,如果你可以从上面的代码中看到为什么标签没有显示......

Thanks

2 个解决方案

#1


UIKit is not thread-safe. If you're updating a UI element, you need to sync back into the main thread or all bets are off.

UIKit不是线程安全的。如果您要更新UI元素,则需要同步回主线程或所有投注都已关闭。

#2


in some cases, I've found that I need to go back to the main thread to do certain things...

在某些情况下,我发现我需要回到主线程来做某些事情......

so in your delegate methods you'd do

所以在你的委托方法中你会这样做

[self performSelectorOnMainThread: @selector(updateLabel:) withObject:newLabelText waitUntilDone:NO];

and then

- (void) updateLabel:(NSString *)newLabelText
{
    [progressLabel setText:newLabelText];
}

I'm not sure what the rules are for what things have to be done on the main thread rather than in the background, though.

我不确定在主线程上而不是在后台执行什么操作的规则是什么。


推荐阅读
  • 如何自行分析定位SAP BSP错误
    The“BSPtag”Imentionedintheblogtitlemeansforexamplethetagchtmlb:configCelleratorbelowwhichi ... [详细]
  • 本文介绍了Python高级网络编程及TCP/IP协议簇的OSI七层模型。首先简单介绍了七层模型的各层及其封装解封装过程。然后讨论了程序开发中涉及到的网络通信内容,主要包括TCP协议、UDP协议和IPV4协议。最后还介绍了socket编程、聊天socket实现、远程执行命令、上传文件、socketserver及其源码分析等相关内容。 ... [详细]
  • 在Android开发中,使用Picasso库可以实现对网络图片的等比例缩放。本文介绍了使用Picasso库进行图片缩放的方法,并提供了具体的代码实现。通过获取图片的宽高,计算目标宽度和高度,并创建新图实现等比例缩放。 ... [详细]
  • IhaveconfiguredanactionforaremotenotificationwhenitarrivestomyiOsapp.Iwanttwodiff ... [详细]
  • 本文主要解析了Open judge C16H问题中涉及到的Magical Balls的快速幂和逆元算法,并给出了问题的解析和解决方法。详细介绍了问题的背景和规则,并给出了相应的算法解析和实现步骤。通过本文的解析,读者可以更好地理解和解决Open judge C16H问题中的Magical Balls部分。 ... [详细]
  • 【MicroServices】【Arduino】装修甲醛检测,ArduinoDart甲醛、PM2.5、温湿度、光照传感器等,数据记录于SD卡,Python数据显示,UI5前台,微服务后台……
    这篇文章介绍了一个基于Arduino的装修甲醛检测项目,使用了ArduinoDart甲醛、PM2.5、温湿度、光照传感器等硬件,并将数据记录于SD卡,使用Python进行数据显示,使用UI5进行前台设计,使用微服务进行后台开发。该项目还在不断更新中,有兴趣的可以关注作者的博客和GitHub。 ... [详细]
  • This article discusses the efficiency of using char str[] and char *str and whether there is any reason to prefer one over the other. It explains the difference between the two and provides an example to illustrate their usage. ... [详细]
  • 本文讨论了微软的STL容器类是否线程安全。根据MSDN的回答,STL容器类包括vector、deque、list、queue、stack、priority_queue、valarray、map、hash_map、multimap、hash_multimap、set、hash_set、multiset、hash_multiset、basic_string和bitset。对于单个对象来说,多个线程同时读取是安全的。但如果一个线程正在写入一个对象,那么所有的读写操作都需要进行同步。 ... [详细]
  • HDU 2372 El Dorado(DP)的最长上升子序列长度求解方法
    本文介绍了解决HDU 2372 El Dorado问题的一种动态规划方法,通过循环k的方式求解最长上升子序列的长度。具体实现过程包括初始化dp数组、读取数列、计算最长上升子序列长度等步骤。 ... [详细]
  • C++字符字符串处理及字符集编码方案
    本文介绍了C++中字符字符串处理的问题,并详细解释了字符集编码方案,包括UNICODE、Windows apps采用的UTF-16编码、ASCII、SBCS和DBCS编码方案。同时说明了ANSI C标准和Windows中的字符/字符串数据类型实现。文章还提到了在编译时需要定义UNICODE宏以支持unicode编码,否则将使用windows code page编译。最后,给出了相关的头文件和数据类型定义。 ... [详细]
  • 在Xamarin XAML语言中如何在页面级别构建ControlTemplate控件模板
    本文介绍了在Xamarin XAML语言中如何在页面级别构建ControlTemplate控件模板的方法和步骤,包括将ResourceDictionary添加到页面中以及在ResourceDictionary中实现模板的构建。通过本文的阅读,读者可以了解到在Xamarin XAML语言中构建控件模板的具体操作步骤和语法形式。 ... [详细]
  • 开发笔记:实验7的文件读写操作
    本文介绍了使用C++的ofstream和ifstream类进行文件读写操作的方法,包括创建文件、写入文件和读取文件的过程。同时还介绍了如何判断文件是否成功打开和关闭文件的方法。通过本文的学习,读者可以了解如何在C++中进行文件读写操作。 ... [详细]
  • 先看官方文档TheJavaTutorialshavebeenwrittenforJDK8.Examplesandpracticesdescribedinthispagedontta ... [详细]
  • iOS Swift中如何实现自动登录?
    本文介绍了在iOS Swift中如何实现自动登录的方法,包括使用故事板、SWRevealViewController等技术,以及解决用户注销后重新登录自动跳转到主页的问题。 ... [详细]
  • 本文由编程笔记#小编整理,主要介绍了关于数论相关的知识,包括数论的算法和百度百科的链接。文章还介绍了欧几里得算法、辗转相除法、gcd、lcm和扩展欧几里得算法的使用方法。此外,文章还提到了数论在求解不定方程、模线性方程和乘法逆元方面的应用。摘要长度:184字。 ... [详细]
author-avatar
雷神白中凌
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有