我正在尝试使用委托在导航堆栈上的子视图和父视图之间传递一些信息。
但是出于某种原因,当我从子视图执行委托然后从导航控制器弹出子视图时,它永远不会进入在父视图中设置的委托方法。我尝试过NSLogs&断点线程在主视图中没有使它成为这个委托方法所以我希望你可以帮助我。
首先,我将告诉你我如何设置我的委托,我在子视图中调用它,然后我在主视图中设置它,希望你们能够看到我到目前为止没有的东西。
Subview.h //我遗漏了与委托无关的事情
#import <UIKit/UIKit.h>
//Delegate - Protocol stuff for passing data from this view to the parent view
@protocol PassSearchData <NSObject>
@required
- (void) setManufactureSearchFields:(NSArray *)arrayValues withIndexPath:(NSIndexPath *)myIndexPath;
@end
@interface SecondViewController : UITableViewController <UITableViewDataSource> {
//Delegate (Used to pass information back to parent view)
id <PassSearchData> delegate;
}
//Delegate (Used to pass information back to parent view)
@property (strong) id delegate;
@end
SecondView.m
#import "SecondViewController.h"
#import "FirstViewController.h"
@implementation SecondViewController
//..
//Delegate (Used to pass information back to parent view)
@synthesize delegate;
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//Access selected cells content (cell.textLabel.text)
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
//Parent view logic (sends info back to the correct cell in parent view)
if (parentViewSelectedIndexPath.section == 0)
{
if (parentViewSelectedIndexPath.row == 0)
{
//Predicates restrict the values that will be returned from the query
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K like %@",@"MANUFACTURER",cell.textLabel.text];
filterArray = [parsedDataArrayOfDictionaries filteredArrayUsingPredicate:predicate];
[[self delegate]setManufactureSearchFields:filterArray withIndexPath:indexPath];
// NSLog(@"Filtered Array = %@", filterArray);
}
}
[self.navigationController popViewControllerAnimated:YES]; //pops current view from the navigatoin stack
}//...
然后我在FirstViewController
中设置它FirstViewController.h
#import <UIKit/UIKit.h>
#import "VehicleResultViewController.h" //With this included I can now use the PassSearchData delegates of this view for passing data
@interface VehicleSearchViewController : UITableViewController <PassSearchData> {
//..
FirstViewController.m
#import "VehicleSearchViewController.h"
#import "VehicleResultViewController.h" //Subview
//..
#pragma mark - Received data from Sub view delegates
//These are the delegate method for passing data from the child to the parent view (parent being this view, with the delegate being declared in the subview)
- (void) setManufactureSearchFields:(NSArray *)arrayValues withIndexPath:(NSIndexPath *)myIndexPath
{
manufactureSearchObjectString = [[arrayValues valueForKey:@"MANUFACTURER"] objectAtIndex:0];
manufactureIdString = [[arrayValues valueForKey:@"MANUFACTURERID"] objectAtIndex:0]; //Restricts Models dataset
manufactureResultIndexPath = myIndexPath;
}
如果有人知道我错过了什么/做错了,那么任何帮助都会真正堆积
任何问题让我知道。
解决方案
在firstviewcontroller中我去加载secondviewcontroller tableView:didSelectRowAtIndexPath:在将视图推送到导航堆栈之前,我忘了设置secondviewcontrollers委托..所以丢失的代码就是这个。
FirstView.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//Get the subview ready for use
VehicleResultViewController *vehicleResultViewController = [[VehicleResultViewController alloc] initWithNibName:@"VehicleResultViewController" bundle:nil];
//Pass the selected object to the new view controller.
[self.navigationController pushViewController:vehicleResultViewController animated:YES];
[vehicleResultViewController setDelegate:self];
//etc
谢谢你们。
答案 0 :(得分:2)
好的,首先,您的委托应该是一个弱引用,以避免保留周期。但我怀疑代表可能没有设定。你能展示实例化SecondViewController并设置委托的代码吗?你能在委托电话中设置一个断点并验证它不是零吗?
答案 1 :(得分:1)
我认为你没有设置代表。 在创建SecondViewController的实例(对象)之后,您必须设置Delegate。
即
SecondViewController *secondObj=[[SecondViewController alloc]initWithNibName:@"SecondViewController"> bundle:nil];
[secondObj setDelegate:self];