iphone - 当我确实需要使用 [NSThread sleepForTimeInterval :1];

标签 iphone objective-c multithreading

我有一个程序需要使用 sleep 。就像真的需要那样。与其花很多时间解释原因,不如说它需要它。

现在我被告知,如果需要 sleep ,请将代码拆分到一个单独的线程中,这样我就不会失去界面响应能力,因此我开始学习如何使用 NSThread。我创建了一个全新的概念性程序,因此解决此示例的问题将有助于我的实际程序。

简短的故事是我有一个类,它有实例变量,我需要一个带有 sleep 的循环来依赖于该实例变量的值。

这就是我整理的内容,非常感谢您的帮助:)

干杯

蒂姆

/// Start Test1ViewController.h ///
#import <UIKit/UIKit.h>

@interface Test1ViewController : UIViewController {
 UILabel* label;
}
@property (assign) IBOutlet UILabel *label;
@end
/// End Test1ViewController.h ///

/// Start Test1ViewController.m ///
#import "Test1ViewController.h"
#import "MyClass.h"
@implementation Test1ViewController
@synthesize label;
- (void)viewDidAppear:(BOOL)animated {
 [super viewDidAppear:animated];
 label.text = @"1";
 [NSThread detachNewThreadSelector:@selector(backgroundProcess) toTarget:self withObject:nil];
}

- (void)backgroundProcess {
 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];  

 // Create an instance of a class that will eventually store a whole load of variables
 MyClass *aMyClassInstance = [MyClass new]; [aMyClassInstance createMyClassInstance:(@"Timbo")];

 while (aMyClassInstance.myVariable--) {
  NSLog(@"blah = %i",aMyClassInstance.myVariable);
  label.text = [NSString stringWithFormat:@"blah = %d", aMyClassInstance.myVariable];

  //how do I pass the new value out to the updateLabel method, or reference aMyClassInstance.myVariable?
  [self performSelectorOnMainThread:@selector(updateLabel) withObject:nil waitUntilDone:NO]; 

  //the sleeping of the thread is absolutely mandatory and must be worked around.  The whole point of using NSThread is so I can have sleeps

  [NSThread sleepForTimeInterval:1];
 }
 [pool release];
}

- (void)updateLabel {label.text = [NSString stringWithFormat:@"blah = %d", aMyClassInstance.myVariable]; // be nice if i could }
- (void)didReceiveMemoryWarning {[super didReceiveMemoryWarning];}
- (void)viewDidUnload {}
- (void)dealloc {[super dealloc];}
@end
/// End Test1ViewController.m ///

/// Start MyClass.h ///
#import <Foundation/Foundation.h>
@interface MyClass : NSObject {
 NSString* name;
 int myVariable;
}
@property int myVariable;
@property (assign) NSString *name;
- (void) createMyClassInstance: (NSString*)withName;
- (int) changeVariable: (int)toAmount;
@end
/// End MyClass.h ///

/// Start MyClass.h ///
#import "MyClass.h"
@implementation MyClass
@synthesize name, myVariable;
- (void) createMyClassInstance: (NSString*)withName{
 name = withName;
 myVariable = 10;
}
- (int) changeVariable: (int)toAmount{ 
 myVariable = toAmount;
 return toAmount;
}
@end
/// End MyClass.h ///

最佳答案

您可以在将 myVariable 值传递到主线程时对其进行包装,如下所示:

[self performSelectorOnMainThread:@selector(updateLabel:) withObject:[NSNumber numberWithInt:aMyClassInstance.myVariable] waitUntilDone:NO]; 

然后,您必须稍微修改 updateLabel 方法,如下所示:

- (void)updateLabel:(NSNumber *)arg {
    int value = [arg intValue];
    label.text = [NSString stringWithFormat:@"blah = %d", value];
}

希望有帮助。

关于iphone - 当我确实需要使用 [NSThread sleepForTimeInterval :1];,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2956175/

相关文章:

iphone - 在iPhone中不使用AVCaptureSession调用UIImagePickerControl的方法

iphone - iPhone 操作系统上的 NSCurrentLocaleDidChangeNotification

objective-c - 如何杀死所有不是主线程的线程?

c++ - 如何设置 "this"线程的自定义名称?

delphi - 如果终止挂起的线程是个好主意,那么我该如何安全地执行它?

java - 在 AsyncTask 进程中检索位图时发生错误

iphone - UITableViewCells 使用 textViewDidEndEditing 将数据写入 plist

iphone - 如何在 IOS6 中为 MPMoviePlayerController 仅提供一个景观 View

iphone - iOS5 和 iOS 5.1 中按钮上缺少字体文本

ios - 在 iOS 中管理动画