代表无法正常工作
我想通过委托在导航堆栈上的子视图和父视图之间传递一些信息。代表无法正常工作
但是,由于某种原因,当我从子视图执行委托,然后从导航控制器弹出子视图它永远不会进入在父视图中设置的委托方法..我已经尝试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;
}
如果有人知道我缺少/做错了任何帮助将是真的真的heapful什么
任何问题,让我知道。
解决方案
在当我去加载内 的tableView的secondviewcontroller的firstviewcontroller: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
感谢你们。
好的,首先,你的代表应该是一个弱引用来避免保留周期。但我怀疑也许代表没有被设置。你可以在SecondViewController被实例化并且委托被设置的地方显示代码吗?你可以在委托调用中设置一个断点并确认它不是零吗?
我想你不是在设置委托。 创建SecondViewController的Instanace(对象)后,您必须设置Delegate。
即
SecondViewController *secondObj=[[SecondViewController alloc]initWithNibName:@"SecondViewController"> bundle:nil];
[secondObj setDelegate:self];
的确,这里的问题是我没有设置委托的事实。感谢您的帮助。 – 2012-01-09 19:56:08
好吧,我改变了我的参考弱,但现在我得到这个奇怪的错误。 ** _weak property'delegate'的现有ivar'委托'必须是_weak **另外,我并不是100%确定您所指的是您要求我向您展示SecondViewController实例化并且委托的代码设置..你是指当我从第一个viewcontroller加载第二个viewcontroller到导航堆栈? – 2012-01-09 19:50:06
这正是你所指的太..我已经解决了这个问题现在与上面添加的解决方案..谢谢你的帮助...完全设法让一个自己哈哈。 – 2012-01-09 19:55:35
很高兴你解决了它。你是否也获得了_weak ivar参考?您可以尝试移除伊娃尔声明并让编译器为您生成它。您还应该在属性声明的id后添加。 –
Jerry
2012-01-09 20:47:24