OGeek|极客世界-中国程序员成长平台

标题: iphone - 委托(delegate)无法正常工作 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 10:37
标题: iphone - 委托(delegate)无法正常工作

我正在尝试使用委托(delegate)在导航堆栈上的 subview 和父 View 之间传递一些信息。

但是由于某种原因,当我从 subview 执行委托(delegate)然后从导航 Controller 弹出 subview 时,它永远不会进入在父 View 中设置的委托(delegate)方法。我已经尝试过 NSLogs & Breakpoints 线程是挑衅没有在主视图中使用这个委托(delegate)方法,所以我希望你能帮助我。

首先,我将向您展示我是如何设置我的代理的,我在 subview 中调用它,然后我在主视图中设置它,希望你们能够看到我到目前为止还没有看到的东西。

Subview.h//我省略了与委托(delegate)无关的内容

#import <UIKit/UIKit.h>

//Delegate - Protocol stuff for passing data from this view to the parent view
@protocol PassSearchData <NSObject>
@required
- (void) setManufactureSearchFieldsNSArray *)arrayValues withIndexPathNSIndexPath *)myIndexPath;
@end

@interface SecondViewController : UITableViewController <UITableViewDataSource> {
//Delegate (Used to pass information back to parent view)
    id <assSearchData> 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)tableViewUITableView *)tableView didSelectRowAtIndexPathNSIndexPath *)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 <assSearchData> {
//..

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) setManufactureSearchFieldsNSArray *)arrayValues withIndexPathNSIndexPath *)myIndexPath
{
    manufactureSearchObjectString = [[arrayValues valueForKey"MANUFACTURER"] objectAtIndex:0];
    manufactureIdString = [[arrayValues valueForKey"MANUFACTURERID"] objectAtIndex:0]; //Restricts Models dataset
    manufactureResultIndexPath = myIndexPath;
}

如果有人知道我遗漏了什么/做错了什么,那么任何帮助都会非常有帮助

如有任何问题,请告诉我。

解决方案

在firstviewcontroller里面的时候我去加载secondviewcontroller里面 tableView:didSelectRowAtIndexPath: 在将 View 推送到导航堆栈之前,我忘记设置 secondviewcontrollers 委托(delegate)。所以缺少的代码是这样的。

FirstView.m

- (void)tableViewUITableView *)tableView didSelectRowAtIndexPathNSIndexPath *)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

谢谢大家。



Best Answer-推荐答案


好的,首先,您的委托(delegate)应该是一个弱引用以避免保留循环。但我怀疑委托(delegate)可能没有准备好。您能否显示实例化 SecondViewController 并设置委托(delegate)的代码?您可以在委托(delegate)调用处设置断点并验证它不是零吗?

关于iphone - 委托(delegate)无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8783269/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4