2012-01-09 34 views
2

我想通過委託在導航堆棧上的子視圖和父視圖之間傳遞一些信息。代表無法正常工作

但是,由於某種原因,當我從子視圖執行委託,然後從導航控制器彈出子視圖它永遠不會進入在父視圖中設置的委託方法..我已經嘗試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 

感謝你們。

回答

2

好的,首先,你的代表應該是一個弱引用來避免保留週期。但我懷疑也許代表沒有被設置。你可以在SecondViewController被實例化並且委託被設置的地方顯示代碼嗎?你可以在委託調用中設置一個斷點並確認它不是零嗎?

+0

好吧,我改變了我的參考弱,但現在我得到這個奇怪的錯誤。 ** _weak property'delegate'的現有ivar'委託'必須是_weak **另外,我並不是100%確定您所指的是您要求我向您展示SecondViewController實例化並且委託的代碼設置..你是指當我從第一個viewcontroller加載第二個viewcontroller到導航堆棧? – 2012-01-09 19:50:06

+0

這正是你所指的太..我已經解決了這個問題現在與上面添加的解決方案..謝謝你的幫助...完全設法讓一個自己哈哈。 – 2012-01-09 19:55:35

+0

很高興你解決了它。你是否也獲得了_weak ivar參考?您可以嘗試移除伊娃爾聲明並讓編譯器爲您生成它。您還應該在屬性聲明的id後添加。 – Jerry 2012-01-09 20:47:24

1

我想你不是在設置委託。 創建SecondViewController的Instanace(對象)後,您必須設置Delegate。

SecondViewController *secondObj=[[SecondViewController alloc]initWithNibName:@"SecondViewController"> bundle:nil]; 

[secondObj setDelegate:self]; 
+0

的確,這裏的問題是我沒有設置委託的事實。感謝您的幫助。 – 2012-01-09 19:56:08