1

我需要一个简单的 iPhone 上的 pinger。所以我尝试让SimplePing 示例 在 iPhone 上运行。但是当我像示例中那样初始化 pinger 时,主运行循环不会处理 SimplePing 生成的事件。这是初始化代码:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

_pinger = [SimplePing simplePingWithHostName:@"192.168.210.1"];
_pinger.delegate = self;
[_pinger start];
NSLog(@"Pinger started");
[self.window makeKeyAndVisible];
return YES; }

如果我直接在这个函数中触发 runloop,如下所示:

do {
    [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
} while (_pinger != nil);

有用。所以我的问题是,为什么事件没有在主运行循环中处理UIApplicationMain,我必须做什么才能让主运行循环完成?

4

1 回答 1

0

好吧,做这样的事情:

-(void)stopPinging
{

 NSLog(@"STOP");

 self.pinger = nil;  // or _pinger = nil in your case so that the while loop doesn't   execute again.

 [self.pinger stop]; // this method will call the Simpleping class method stop which takes care of the rest ..


}
于 2011-11-14T15:06:38.423 回答