• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

iphone - 委托(delegate)无法正常工作

[复制链接]
菜鸟教程小白 发表于 2022-12-13 10:37:42 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我正在尝试使用委托(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/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap