gpt4 book ai didi

iphone - 无法识别的选择器,委托(delegate)问题

转载 作者:行者123 更新时间:2023-11-29 13:41:50 25 4
gpt4 key购买 nike

我收到这个错误

[ResultViewController setSearchFields:IndexPath:]: unrecognized selector sent to instance 0xc448840

我有一个导航 Controller 堆栈,我在其中使用委托(delegate)将信息传递回导航堆栈中的前一个 View 。但是我认为我在声明委托(delegate)时做错了什么。

我的导航栈看起来像这样。

view 0 (mainmenu)
-- view 1 (SearchViewController)
--- view 2 (ResultViewController) - where I set the delegate of the new view being loaded
---- View 3 (SubViewController) - this is where my delegates reside

我正在做的是弹出到 view1 并将委托(delegate)信息传递给该 View ,但是这样做我得到了这个错误......我想知道我是否必须在我结束的 View 1 中为 View 3 设置委托(delegate)传递信息...是正确的吗?

如果是这样,在设置委托(delegate)时我需要考虑什么?我如何从 View 1 调用它

这就是我在 SubViewController 中设置委托(delegate)的方式

subvc.h

@protocol PassSubSearchData <NSObject>
@required
- (void) setSearchFields:(NSArray *)modArray IndexPath:(NSIndexPath *)modIndexPath;
@end

@interface VehicleSubResultViewController : UITableViewController <NSXMLParserDelegate> {
//..
//Delegate for passing Mod and SubMod data back to VehicleSearchViewController
id <PassSubSearchData> delegate;
//..
//Delegate for passing Mod and SubMod data back to VehicleSearchViewController
@property (strong) id delegate;

subvc.m

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//Access selected cells content (cell.textLabel.text)
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

//Predicates restrict the values that will be returned from the query.
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K like %@",@"SUB",cell.textLabel.text];
filterArray = [parsedDataArrayOfDictionaries filteredArrayUsingPredicate:predicate];

[[self delegate] setSearchFields:tempModArray IndexPath:tempModIndexPath];

//This pops to the View 1 - SearchViewController
[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:1] animated:YES];

}

然后在我的 SearchViewController 中,这就是我设置委托(delegate)的方式

searchvc.h

#import "SubViewController.h"

@interface SearchViewController : UITableViewController <PassSubSearchData> {
//..

searchvc.m

- (void) setSearchFields:(NSArray *)modArray IndexPath:(NSIndexPath *)modIndexPath
{
modSearchObjectString = [[modArray valueForKey:@"MOD"] objectAtIndex:0];
modSearchIndexPath = modIndexPath; // Sets the selected IndexPath from the subview


NSLog(@"%@", modSearchObjectString);
NSLog(@"%@", modResultIndexPath);



[self.tableView reloadData];
}

总结得差不多了..抱歉耽搁了。

最佳答案

据我所知,将信息存储到一个文件中,然后在需要时将其取回可能是实现您想要的目标的更简单方法。像这样的东西(我知道,过于简单化):

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//store the cell's title into a string
NSString* string = [[[tableView cellForRowAtIndexPath:indexPath] textLabel] text];

[[NSUserDefaults standardUserDefaults] setObject:string forKey:@"someKey"];
}

稍后您可以使用以下方法取回该字符串:

NSString* titleString = [[NSUserDefaults standardUserDefaults] objectForKey:@"someKey"];

您可以用同样的方式处理您需要传输的信息。

另一种方法是在接收数据的 View Controller 中创建一个属性:

@property(nonatomic, retain) NSString* string; //you need to synthesize it in the .m file too

然后在弹出 View Controller 之前:

//make sure you cast it
(SearchViewController*)[self.navigationController.viewControllers objectAtIndex:1].string = @"some string";

然后一旦你转到该 Controller ,该属性将被设置为你在

中设置的任何字符串
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

通过这些方式,您可以在 View Controller 之间传递信息。祝你好运!

关于iphone - 无法识别的选择器,委托(delegate)问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8873113/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com