這也是我需要解析參數到NSTimer。所以我想出了創建類別班,這對我有幫助。
以下是通過NSTimer創建Category類的步驟。
- 右鍵單擊項目,選擇 「新建文件」
- Cacoa觸摸>的Objective-C類>接下來
- 給範疇:補充&類別上:
中的NSTimer
- .h文件中只創建propertyId
- in .m使用@dynamic合成它&創建setter,getter
- 將.h文件導入到您的文件中。
的NSTimer +使用的NSTimer additions.h文件
#import <Foundation/Foundation.h>
@interface NSTimer (additions)
@property(nonatomic,retain) NSString *additionalTag;
@end
的NSTimer + additions.m文件
#import "NSTimer+additions.h"
#import <objc/runtime.h>
NSString *const additionalTagKey = @"additionalTagKey";
@implementation NSTimer (additions)
@dynamic additionalTag;
- (void)setAdditionalTag:(NSString*)aObject
{
objc_setAssociatedObject(self, additionalTagKey, aObject, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (NSString*)additionalTag
{
return objc_getAssociatedObject(self, additionalTagKey);
}
@end
現在#import "NSTimer+additions.h"
在類
從計時器
-(void)demoMethod:(NSTimer *)timer{
NSLog(@"My Parameter: %@",timer.additionalTag);
}
許多步驟都參與,但希望你有點幫助傳遞參數的NSTimer
-(void)testDemoMethodPassTimer{
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0f
target:self
selector:@selector(timerTick:)
userInfo:nil
repeats:YES];
timer.additionalTag = @"parameter passed to timer";
ClassA *testA = [[ClassA alloc]init];
STAssertNoThrows([testA testDemoMethod:timer]);
}
檢索參數。 :)
是否要將參數傳遞給NSTimer以訪問demoMethod:方法? – 2013-02-28 06:36:45
@Bhargavi是的! – Kiran 2013-02-28 09:04:09