[英]using a protocol & delegate to pass data between views
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')"
“找不到+ setDelegate:”(返回类型默认为“ 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 更新后,这就是我将我的代理设置为SecondView.h的方式
//...
@interface VehicleResultViewController : UITableViewController <NSXMLParserDelegate> {
//..
id <PassSearchData> delegate;
}
//..
@property (retain) id delegate;
@end
SecondView.m SecondView.m
//..
@synthesize delegate;
//..
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[[self delegate] setVehicleSearchFields:vehicleCellTextLabel];
}
//..
I hope this better clarifies what I am doing. 我希望这能更好地阐明我在做什么。
You are calling an instance method on a class. 您正在类上调用实例方法。
This line: 这行:
[VehicleResultViewController setDelegate:self];
should be: 应该:
[vehicleResultViewController setDelegate:self];
The first line calls setDelegate on VehicleResultViewController, which is the name of the class. 第一行在VehicleResultViewController上调用setDelegate,这是类的名称。 Since setDelegate is not a class method the compiler complains.
由于setDelegate不是类方法,因此编译器会抱怨。
The corrected line calls setDelegate on vehicleResultViewController, which is an instance of the class that you allocated. 更正后的行在vehicleResultViewController上调用setDelegate,这是您分配的类的实例。 This will work because setDelegate is an instance method.
这将起作用,因为setDelegate是实例方法。
It seems that you have declared a setter for delegate
as a class method, indicated by the +
in the warning. 似乎您已将
delegate
的setter声明为类方法,在警告中用+
表示。
I'm guessing you have this declared somewhere... 我猜你在某处声明了这个...
+ (void)setDelegate:(id <SomeProtocol>)aDelegate
when it should be - (void)setDelegate:(id <SomeProtocol>)aDelegate
+ (void)setDelegate:(id <SomeProtocol>)aDelegate
何时应- (void)setDelegate:(id <SomeProtocol>)aDelegate
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.