【发布时间】:2015-03-18 03:59:15
【问题描述】:
免责声明:我在谈论代表时使用的术语可能有点不妥。
虽然我发现了一些有趣的行为,但我已经调试了几个小时了,但还没有得到任何结果。基本上我有一个带有自定义 NSObject (NSO) 实例作为属性的 UIViewController (VC)。我在 NSO 对象中创建了一个委托,以便它可以在 VC 中通话/调用函数。我以前这样做过,所以我认为没问题,但是当我尝试从 NSO 对象调用委托函数时,VC 没有收到调用。
这是我的 NSO 类界面
#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>
#import <Parse/Parse.h>
#import <MobileCoreServices/MobileCoreServices.h>
@protocol ServerDBDelegate <NSObject>
@required
- (void) func;
@end
@interface ServerDB : NSObject <CLLocationManagerDelegate>
@property (nonatomic, retain) id<ServerDBDelegate> delegate;
@end
NSO 对象使用 CoreLocation,我从 CoreLocation 委托函数之一调用 VC 委托函数,特别是...
-(void)locationManager: didUpdateLocations: {
[self.delegate func];
}
如果这令人困惑,请发表评论,我会澄清。
由于 VC 从未收到我在这一行中放入上述函数的调用。
NSLog(self.delegate == nil ? @"Nil" : @"Not nil");
而且每次都会打印“Nil”,所以我认为我没有正确设置我的代表。经过更多调试后,我仍然认为我正确地执行了委托,所以我重写了委托的设置器,以查看它是否在某个时候被正确设置。
- (void) setDelegate:(id<ServerDBDelegate>)delegate {
_delegate = delegate;
NSLog(self.delegate == nil ? "Nil2" : "Not Nil2");
}
当 setter 被调用时,它输出了“Not Nil2”,然后一瞬间我看到了 CoreLocation 委托函数的输出“Nil”。所以我认为我最初正确设置了委托,但在某些时候它被覆盖为零。好的,我的最后一步。
- (void) setDelegate:(id<ServerDBDelegate>)delegate {
_delegate = delegate;
[self performSelector:@selector(test) withObject:self afterDelay:3];
}
- (void) test {
NSLog(self.delegate == nil ? "Nil2" : "Not Nil2");
}
-(void)locationManager: didUpdateLocations: {
NSLog(self.delegate == nil ? "Nil" : "Not Nil");
[self test];
}
重要的是要知道,locationManager 函数每秒会被调用几次。所以当我运行它时,我会看到这个输出
Nil
Nil2
Nil
Nil2
Nil
Nil2
Nil
Nil2
Not Nil2
Nil
Nil2
所以看起来委托没有设置,但是当我在延迟后从设置器调用 test: 时,它仍然设置,但仅在该函数的范围内。我真的不知道如何进行或错误可能在哪里。提前感谢您的帮助。
【问题讨论】:
-
您在哪里/如何声明您的 _delegate?
-
我会将其添加为编辑。
-
您作为代表分配了什么?该类是否符合您的委托?
-
VC 的声明如下所示:@interface ViewBarsViewController : UIViewController
我将 NSO 实例委托设置为 UIViewController -
你是怎么调用
setDelegate方法的?
标签: ios delegates core-location