Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c Cocoa-setAction的Pass参数_Objective C_Cocoa - Fatal编程技术网

Objective c Cocoa-setAction的Pass参数

Objective c Cocoa-setAction的Pass参数,objective-c,cocoa,Objective C,Cocoa,我想我应该尝试创建一个简单的可可应用程序。它是reddit的一个简单的收件箱通知程序。我带来了一堆网址,并希望为每个网址与网页链接菜单项。我想动态地设置每个的操作。我需要将URL传递给该方法,以便它知道要去哪里。我有一种感觉,我做错了。有人能给我指一下正确的方向吗。我只想创建一个NSURL并将其发送到loadMessage NSURL *tempURL = [NSURL URLWithString:[NSString stringWithFormat:@"http://www.reddit

我想我应该尝试创建一个简单的可可应用程序。它是reddit的一个简单的收件箱通知程序。我带来了一堆网址,并希望为每个网址与网页链接菜单项。我想动态地设置每个的操作。我需要将URL传递给该方法,以便它知道要去哪里。我有一种感觉,我做错了。有人能给我指一下正确的方向吗。我只想创建一个NSURL并将其发送到loadMessage

NSURL *tempURL   = [NSURL  URLWithString:[NSString stringWithFormat:@"http://www.reddit.com%@", [[message objectForKey:@"data"] objectForKey:@"context"]]];

[temptItem setAction:@selector(loadMessage:messageUrl:)];

该选择器不是有效的操作消息。操作可以接受一个参数,也可以不接受任何参数;如果它们接受一个参数,则参数中传递的对象将是发送消息的控件


您需要做的是在控制器中创建一个方法,用正确的对象调用
loadMessage:messageURL:
方法。

正如Chuck所说,选择器的形式错误。一种方法是使用
-representedObject
将项目与URL关联:

- (void)menuAction:(id)sender {
    [[NSWorkspace sharedWorkspace] openURL:[sender representedObject]]; 
}

// adding an item:
NSURL *url = [NSURL URLWithString:@"http://google.com/"];    
NSMenuItem *item = [[[NSMenuItem alloc] initWithTitle:@"moo" 
                    action:@selector(menuAction:) keyEquivalent:@""] autorelease];
[item setTarget:self];
[item setRepresentedObject:url];
[item setEnabled:YES];    
// insert into menu

好的,但是我如何将URL与每个菜单项相关联呢?我尝试了这个,虽然没有出错,但也不太管用。我用下面的方法声明尝试了你的建议。[code]-(iAction)menuLoadMessage:(id)sender{[self-loadMessage:[sender-representedObject]];(void)loadMessage:(NSURL*)messageUrl{[[NSWorkspace-sharedWorkspace]openURL:messageUrl];}[/code]@user:您对项目使用了
-setRepresentedObject:
吗?NSURL*tempull=[nsurlwithstring:[NSString stringWithFormat:@”“,[[message objectForKey:@“data”]objectForKey:@“context”]];[tempItem setRepresentedObject:tempURL];[tempItem setAction:@selector(MenuLadMessage:)];user312867:该代码正在运行吗(即,它正在被调用的是方法/块/复合语句)?您是否验证了
tempItem
不是
nil
?很好。好的。我没有将目标设置为self。这样做背后的逻辑是什么?很抱歉…..对这件事来说太陌生了。。。