Skip to content

Commit d650b55

Browse files
committed
updated NSTimerBlocks
1 parent 21d080c commit d650b55

File tree

4 files changed

+65
-9
lines changed

4 files changed

+65
-9
lines changed

BackgroundImage/MainWindow.xib

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
</object>
1313
<object class="NSMutableArray" key="IBDocument.EditedObjectIDs">
1414
<bool key="EncodedWithXMLCoder">YES</bool>
15+
<integer value="2"/>
1516
<integer value="9"/>
16-
<integer value="18"/>
1717
</object>
1818
<object class="NSArray" key="IBDocument.PluginDependencies">
1919
<bool key="EncodedWithXMLCoder">YES</bool>
@@ -49,7 +49,7 @@
4949
<object class="IBUIImageView" id="889419665">
5050
<reference key="NSNextResponder" ref="380026005"/>
5151
<int key="NSvFlags">1298</int>
52-
<string key="NSFrame">{{0, 20}, {320, 460}}</string>
52+
<string key="NSFrame">{{0, 64}, {320, 416}}</string>
5353
<reference key="NSSuperview" ref="380026005"/>
5454
<bool key="IBUIUserInteractionEnabled">NO</bool>
5555
<string key="targetRuntimeIdentifier">IBCocoaTouchFramework</string>
@@ -238,7 +238,7 @@
238238
<bool key="EncodedWithXMLCoder">YES</bool>
239239
</object>
240240
</object>
241-
<string>{{329, 195}, {320, 480}}</string>
241+
<string>{{251, 195}, {320, 480}}</string>
242242
<string>com.apple.InterfaceBuilder.IBCocoaTouchPlugin</string>
243243
<string>BackgroundImageAppDelegate</string>
244244
<string>com.apple.InterfaceBuilder.IBCocoaTouchPlugin</string>

NSTimerBlocks/Classes/NSTimerBlocksViewController.m

+30-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,36 @@ - (void)loadView {
3535
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
3636
- (void)viewDidLoad {
3737
[super viewDidLoad];
38+
39+
NSArray* array = [NSArray arrayWithObjects:@"DOG", @"CAT", @"MONKEY", nil];
3840

41+
timer1 = [NSTimer scheduledTimerWithTimeInterval:1.0
42+
block:^(NSTimer* timer) {
43+
self.counterLabel1.text =
44+
[NSString stringWithFormat:@"%d", counter1++];
45+
NSLog(@"%@", array);
46+
}
47+
repeats:YES];
48+
49+
timer2 = [NSTimer scheduledTimerWithTimeInterval:2.0
50+
block:^(NSTimer* timer) {
51+
self.counterLabel2.text =
52+
[NSString stringWithFormat:@"%d", counter2];
53+
counter2 += 10;
54+
NSLog(@"%@", array);
55+
}
56+
repeats:YES];
57+
58+
timer3 = [NSTimer scheduledTimerWithTimeInterval:3.0
59+
block:^(NSTimer* timer) {
60+
self.counterLabel3.text =
61+
[NSString stringWithFormat:@"%d", counter3];
62+
counter3 += 100;
63+
NSLog(@"%@", array);
64+
}
65+
repeats:YES];
66+
67+
/* old version
3968
timer1 = [NSTimer scheduledTimerWithTimeInterval:1.0
4069
block:^(NSTimer* timer, id userInfo) {
4170
self.counterLabel1.text =
@@ -61,7 +90,7 @@ - (void)viewDidLoad {
6190
}
6291
userInfo:nil
6392
repeats:YES];
64-
93+
*/
6594
}
6695

6796

NSTimerBlocks/Classes/NSTimer_Extension.h

+5-2
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@
88

99
#import <Foundation/Foundation.h>
1010

11-
typedef void (^TIMER_BLOCK__)(NSTimer*, id);
11+
typedef void (^TIMER_BLOCK__)(NSTimer*);
1212

1313
@interface NSTimer (Extension)
1414

15-
+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)seconds block:(TIMER_BLOCK__)block userInfo:(id)userInfo repeats:(BOOL)repeats;
15+
//+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)seconds block:(TIMER_BLOCK__)block userInfo:(id)userInfo repeats:(BOOL)repeats;
16+
17+
+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)seconds block:(TIMER_BLOCK__)block repeats:(BOOL)repeats;
18+
1619
@end

NSTimerBlocks/Classes/NSTimer_Extension.m

+27-3
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88

99
#import "NSTimer_Extension.h"
1010

11-
#define KEY_BLOCK @"block"
12-
#define KEY_USERINFO @"userInfo"
13-
1411
@implementation NSTimer (Extension)
1512

13+
/*
14+
#define KEY_BLOCK @"block"
15+
#define KEY_USERINFO @"userInfo"
1616
+ (void)executeBlock__:(NSTimer*)timer
1717
{
1818
if (![timer isValid]) {
@@ -44,5 +44,29 @@ + (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)seconds block:(TIMER
4444
repeats:repeats];
4545
return timer;
4646
}
47+
*/
48+
49+
+ (void)executeBlock__:(NSTimer*)timer
50+
{
51+
if (![timer isValid]) {
52+
return;
53+
// do nothing
54+
}
55+
56+
TIMER_BLOCK__ block = [timer userInfo];
57+
block(timer);
58+
}
59+
60+
+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)seconds block:(TIMER_BLOCK__)block repeats:(BOOL)repeats
61+
{
62+
NSTimer* timer = [NSTimer scheduledTimerWithTimeInterval:seconds
63+
target:self
64+
selector:@selector(executeBlock__:)
65+
userInfo:[[block copy] autorelease]
66+
repeats:repeats];
67+
return timer;
68+
69+
}
70+
4771

4872
@end

0 commit comments

Comments
 (0)