[英]Informing a custom delegate class in Objective-C
I have successfully set up a delegate in my NSObject
class and I'm able to transfer some data and run some selectors. 我已经在我的NSObject
类中成功设置了一个委托,并且能够传输一些数据并运行一些选择器。
The thing is, I can't understand how do I send some values back to that class where I've set up my delegate. 问题是,我不明白如何将一些值发送回设置委托的那个类。 I have a ViewController, SGDownloader
that has a BOOL
property and I want to check it on the delegate class to make a conditional firing of some selector. 我有一个具有BOOL
属性的ViewController, SGDownloader
,我想在委托类上对其进行检查,以有条件地触发某些选择器。 The thing is, I can't seem to make it work. 问题是,我似乎无法使其正常运行。 I'm using storyboards and here's my code: 我正在使用情节提要,这是我的代码:
-(void)checkTheQueue {
SGDownloader *downloaderInstance = [SGDownloader new];
NSLog((@"Downloader is now %i"), downloaderInstance.isBusy);
BOOL checkBool = downloaderInstance.isBusy;
if (checkBool == NO) {
SEL selector = @selector(checkTheThing);
if (delegate && [delegate respondsToSelector:selector]) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
[delegate performSelector:selector];
#pragma clang diagnostic pop
}
}
else {
NSLog(@"Sorry, I'm busy");
}
}
I fire the selector checkTheThing
in my first class, the class I'd like to get the isBusy
value from. 我在第一个类中触发选择器checkTheThing
,该类是我想要从其中获取isBusy
值的类。 It's also an @optional
delegate method. 这也是一个@optional
委托方法。 The thing is, that the BOOL
value is always 0, and if I try to pass a string it's always nil
. 事实是, BOOL
值始终为0,如果我尝试传递字符串,则始终为nil
。
I suspect there's a problem with this line - SGDownloader *downloaderInstance = [SGDownloader new];
我怀疑这行有问题SGDownloader *downloaderInstance = [SGDownloader new];
maybe the storyboard get's mixed up or something like that. 也许故事板变得混乱不清或类似的东西。
Edit: 编辑:
Part of the code from SGDownloader
: SGDownloader
部分代码:
- (IBAction)attemptNewDownload:(id)sender {
SGQueueController *myProtocol = [SGQueueController new];
myProtocol.delegate = self;
NSLog((@"Busy value equals %i while checking the protocol"), isBusy);
[myProtocol checkTheQueue];
}
Here NSLog
tells me that the value is 1. Seconds later it's 0 on my protocol class. 在这里, NSLog
告诉我该值为1。几秒钟后,在我的协议类中为0。 Although nothing is done with it later on. 尽管以后什么也没做。
Your Code should work, but you're looking at the wrong Place. 您的代码应该可以工作,但是您在寻找错误的地方。
isBusy
gets set in viewDidLoad
of SGDownloader, right? isBusy
在SGDownloader的viewDidLoad
中设置,对吗? In attemptNewDownload
you create an Instance of SGQueueController. 在attemptNewDownload
您将创建attemptNewDownload
的实例。 Then you set its Delegate to the already created SGDownloader and call checkTheQueue
on it. 然后,将其Delegate设置为已创建的SGDownloader,并在其上调用checkTheQueue
。
In checkTheQueue
you create a new Instance of SGDownloader, but new
doesn't call viewDidLoad
(by default). 在checkTheQueue
您将创建一个新的SGDownloader实例,但是new
不会调用viewDidLoad
(默认情况下)。 So the isBusy
-property of the newly created SGDownloader-Instance is still NO. 因此,新创建的SGDownloader-Instance的isBusy
属性仍然为NO。 Just let out the [SGDownloader new]
-line and change 只需松开[SGDownloader new]
并进行更改
BOOL checkBool = downloaderInstance.isBusy;
to 至
BOOL checkBool = delegate.isBusy;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.