gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 21:23:10 29 4
gpt4 key购买 nike

我有一个程序需要使用 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/

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