objective-c - 无法从委托(delegate)访问 UI?

标签 objective-c cocoa

我得到了这个代码:

GrooveOnDownload *dlg = [[GrooveOnDownload alloc] init];

NSURLDownload *dw = [[NSURLDownload alloc] initWithRequest:request delegate:dlg];

它在带有 UI 控件导出的委托(delegate)类中启动下载。但由于某种原因,控件不响应来自委托(delegate)的直接消息。

//Header of the delegate
@interface GrooveOnDownload : NSObject {
IBOutlet id downloadButton;
//...

//Implementation 
//...
[downloadButton setEnabled:FALSE]; // Doesn't work
//...

有什么想法吗?

这是我进行 Mac 开发的第四天,所以我对该平台了解不多。

编辑: 2010-05-28 01:03:41.486 GrooveOnLite[3303:a0f] 下载按钮 = (null)

编辑2:

alt text

编辑 3: 我想念Windows......

编辑4 在 Win32 中,您向按钮发送一条窗口消息。一切都由 WndProc 循环处理。这太简单了。在 Mac 中,你有这个神奇的界面生成器,它以某种方式让所有这些废话都工作起来。代表被一些魔法召唤。其余类别通过某种“神奇”力量联系在一起。在 Windows 中,有一个 tmain 函数作为入口点。就是这样!没有落后的导出和诸如此类的狗屎。

最佳答案

我知道这听起来很明显,但是 Interface Builder 中的所有连接都正确吗?

编辑

如果下载是在单独的线程上进行的,则 fbrereto 是正确的,您需要在主线程上执行选择器。您的代码需要更改为如下所示:

[downloadButton performSelectorOnMainThread:@selector(setEnabled:)
    withObject:[NSNumber numberWithBool:NO]
    waitUntilDone:YES];

一些注意事项:在 Objective-C 中,使用关键字 NO 而不是 FALSE。它是一种原始类型,因此为了在这里使用它,我们必须将其装箱在 NSNumber 对象中。 waitUntilDone 参数完全符合您的预期,如果您不想等待,可以将其更改为 NO

编辑2

这里有一个更完整的代码示例,说明如何完成我认为您想要的事情,即从应用程序委托(delegate)中重用 GrooveOnDownload 的单个实例。我假设您的应用程序委托(delegate)类名为 GrooveOnLiteAppDelegate。

// GrooveOnLiteAppDelegate.h
@interface GrooveOnLiteAppDelegate : NSObject
{
    IBOutlet GrooveOnDownload *grooveOnDownload;
    // other properties go here
}

// your method signatures go here
@end

// GrooveOnLiteAppDelegate.m
@implementation GrooveOnLiteAppDelegate

- (void)mySuperAwesomeMethod
{
    // it's up to you to figure out what method to put this in and
    // how to call it
    NSURLDownload *dw = [[NSURLDownload alloc] initWithRequest:request delegate:grooveOnDownload];
}

@end

鉴于应用程序委托(delegate)中的代码,您将在 IB 中拥有一个可以连接到 IB 中的 GrooveOnDownload 对象的 socket 。如果您这样做,那么 grooveOnDownload 将是指向该对象的指针。

关于objective-c - 无法从委托(delegate)访问 UI?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2925784/

相关文章:

objective-c - NSWorkspace notificationCenter 未在垃圾收集下发送通知

iphone - 理论上是: How is the alpha component premultiplied into the other components of an PNG in iPhone OS,,如何才能正确地对其进行预乘呢?

ios - 将数组发送到下一个 View Controller

ios - UIScrollView 内容大小固定为 600x600(Main.storyboard 大小)

objective-c - 在 Objective-C 的 CALayer 中绘制圆弧和矩形

objective-c - CALayer : Single pixel line looks like 2 pixels

objective-c - cocoa - 如何以编程方式检索您的应用程序正在使用的内存量?

iphone - CGContext 错误/警告

ios - 如何停止获取?

cocoa - iOS6的目标方向shouldAutorotate