使用协议和委托在视图之间传递数据
我几乎完成了本教程 此处 它确实提供了丰富的信息,帮助我了解协议和委托如何协同工作来传递数据。
然而,当我试图告诉我的子视图主视图是它的委托时,我会弹出一个警告。它发回此警告
“+setDelegate:”未找到(返回类型默认为“id”)”
到目前为止,一切都在按计划进行,我只是想知道这个错误的含义以及它是否与我实现代码的方式有关。
下面是警告发生的地方...
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Navigation logic may go here. Create and push another view controller.
//--- Idendify selected indexPath (section/row)
if (indexPath.section == 0) {
//--- Get the subview ready for use
VehicleResultViewController *vehicleResultViewController = [[VehicleResultViewController alloc] initWithNibName:@"VehicleResultViewController" bundle:nil];
// ...
//--- Sets the back button for the new view that loads
self.navigationItem.backBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"Back" style: UIBarButtonItemStyleBordered target:nil action:nil] autorelease];
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:vehicleResultViewController animated:YES];
[VehicleResultViewController setDelegate:self]; //<<-- warning here says -->> Method'+setDelegate:' not found (return type defaults to 'id')
switch (indexPath.row)
{
case 0: vehicleResultViewController.title = @"Manufacture";
[vehicleResultViewController setRequestString:@"manufacture.php"]; //sets the request string in searchResultsViewController
break;
//...
任何帮助将不胜感激。
更新了,这就是我设置我的代表的方式 SecondView.h
//...
@interface VehicleResultViewController : UITableViewController <NSXMLParserDelegate> {
//..
id <PassSearchData> delegate;
}
//..
@property (retain) id delegate;
@end
SecondView.m
//..
@synthesize delegate;
//..
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[[self delegate] setVehicleSearchFields:vehicleCellTextLabel];
}
//..
我希望这能更好地阐明我在做什么。
I have almost finished working my way through this tutorial here
Its really informative and has helped me understand how protocols and delegates work together for passing data around.
However I have one warning that poping up when I try to tell my subview that the mainview is its delegate. its sending back this warning
"+setDelegate:' not found (return type defaults to 'id')"
Everything was on track up till that point and I am just woundering what this error means and if it is anything to do with the way I have implemented my code.
below is where the warning is happening...
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Navigation logic may go here. Create and push another view controller.
//--- Idendify selected indexPath (section/row)
if (indexPath.section == 0) {
//--- Get the subview ready for use
VehicleResultViewController *vehicleResultViewController = [[VehicleResultViewController alloc] initWithNibName:@"VehicleResultViewController" bundle:nil];
// ...
//--- Sets the back button for the new view that loads
self.navigationItem.backBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"Back" style: UIBarButtonItemStyleBordered target:nil action:nil] autorelease];
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:vehicleResultViewController animated:YES];
[VehicleResultViewController setDelegate:self]; //<<-- warning here says -->> Method'+setDelegate:' not found (return type defaults to 'id')
switch (indexPath.row)
{
case 0: vehicleResultViewController.title = @"Manufacture";
[vehicleResultViewController setRequestString:@"manufacture.php"]; //sets the request string in searchResultsViewController
break;
//...
Any help would be greatly appreciated.
Updated, this is how I set my delegate up
SecondView.h
//...
@interface VehicleResultViewController : UITableViewController <NSXMLParserDelegate> {
//..
id <PassSearchData> delegate;
}
//..
@property (retain) id delegate;
@end
SecondView.m
//..
@synthesize delegate;
//..
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[[self delegate] setVehicleSearchFields:vehicleCellTextLabel];
}
//..
I hope this better clarifies what I am doing.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在调用类的实例方法。
这一行:
应该是:
第一行调用VehicleResultViewController上的setDelegate,这是类的名称。由于 setDelegate 不是类方法,因此编译器会抱怨。
更正后的行调用vehicleResultViewController 上的setDelegate,它是您分配的类的实例。这将起作用,因为 setDelegate 是一个实例方法。
You are calling an instance method on a class.
This line:
should be:
The first line calls setDelegate on VehicleResultViewController, which is the name of the class. Since setDelegate is not a class method the compiler complains.
The corrected line calls setDelegate on vehicleResultViewController, which is an instance of the class that you allocated. This will work because setDelegate is an instance method.
您似乎已将
delegate
的 setter 声明为类方法,如警告中的+
所示。我猜你已经在某处声明了这个...
+ (void)setDelegate:(id)aDelegate
,而它应该是- (void)setDelegate:(id)aDelegate
SomeProtocol>)aDelegateIt seems that you have declared a setter for
delegate
as a class method, indicated by the+
in the warning.I'm guessing you have this declared somewhere...
+ (void)setDelegate:(id <SomeProtocol>)aDelegate
when it should be- (void)setDelegate:(id <SomeProtocol>)aDelegate