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

标签 iphone ios delegates

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

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

首先,我将向您展示如何设置我的委托(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) 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

第二 View .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;
}

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

有任何问题请告诉我。

解决方案

在第一个 View Controller 中,当我去加载第二个 View Controller 里面时 tableView:didSelectRowAtIndexPath: 在将 View 推送到导航堆栈之前,我忘记设置 secondaryviewcontrollers 委托(delegate)。所以缺少的代码是这样的。

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

谢谢大家。

最佳答案

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

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

相关文章:

iphone - iOS : UITableView Reorder control not shown

ios - 如何以编程方式取消选择 UITextField

iphone - iPhone 中的开机事件

ios - SwiftUI AVCapturePhotoOutput 不工作

iphone - 是否可以在 iPhone 上录制/重放一组触摸事件以用于演示目的?

ios - Firebase 在中国的分析

ios - iOS 中的 FireBase removeValue 并不总是持久保存到数据库

c# - 有什么可以用 C# 事件做而不能用 VB 事件做的事情吗?

c# 发出动态方法委托(delegate)以加载参数化构造函数问题

c# - 理解C#中的Begininvoke args