1

我正在尝试实现一个简单的委托,但似乎遇到了一些问题。预期的结果是用户按下UIView链接到UIViewControllervia的按钮Outlet。我也使用UISearchBarDelegatewhich 工作得很好。我只是在调用我的自定义委托方法时遇到问题。

主视图控制器.h:

#import <UIKit/UIKit.h>
#import "CategoryFilterView.h"

@interface MainViewController : UIViewController <UISearchBarDelegate, catFilterDelegate>

@property (strong, nonatomic) IBOutlet UIView *switchViewOutlet;

// Instance Methods
- (void)searchBarLoad;

@end

主视图控制器.m:

#import "MainViewController.h"

@interface MainViewController ()

@end

@implementation MainViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    CategoryFilterView *temp = [[CategoryFilterView alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
    temp.delegate = self;
    self.switchViewOutlet = temp;
    [self.view addSubview:self.switchViewOutlet];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)searchBarLoad {
    UISearchBar *search = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
    search.delegate = self;
    search.showsCancelButton = YES;

    self.switchViewOutlet = search;
    [self.view addSubview:self.switchViewOutlet];

}

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
    [self.switchViewOutlet removeFromSuperview];
    CategoryFilterView *temp = [[CategoryFilterView alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
    temp.delegate = self;
    self.switchViewOutlet = temp;
    [self.view addSubview:self.switchViewOutlet];
}

- (void)searchPressed {
    // Custom Delegate Method
    NSLog(@"Test"); // This is never called
    [self.switchViewOutlet removeFromSuperview];
    [self searchBarLoad];
}

@end

CategoryFilterView.h:

#import <UIKit/UIKit.h>

@protocol catFilterDelegate <NSObject>

- (void)searchPressed;

@end

@interface CategoryFilterView : UIView

// Button Outlets
@property (strong, nonatomic) IBOutlet UIButton *byMeButtonOutlet; // Linked in .xib
@property (strong, nonatomic) IBOutlet UIButton *byFriendsButtonOutlet; // Linked in .xib
@property (strong, nonatomic) IBOutlet UIButton *searchButtonOutlet; // Linked in .xib

// Delegate
@property (assign, nonatomic) id<catFilterDelegate> delegate;

// Button Actions
- (IBAction)searchPressAction:(id)sender; // Linked with UIButton from .xib file

@end

CategoryFilterView.m:

#import "CategoryFilterView.h"

@implementation CategoryFilterView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code        
        NSArray *subviewArray = [[NSBundle mainBundle] loadNibNamed:@"CategoryFilterView" owner:self options:nil];
        UIView *mainView = [subviewArray objectAtIndex:0];
        [self addSubview:mainView];    }
    return self;
}

- (IBAction)searchPressAction:(id)sender {
    NSLog(@"Test"); // This gets called when the user presses the button from MainViewController
    [self.delegate searchPressed];
}
@end

如果需要,我可以提供更多信息。

4

2 回答 2

2

查看您的委托是否响应您尝试调用的选择器是一个好习惯。

我建议使用以下代码调用它:

if([self.delegate respondsToSelector:@selector(searchPressed)])
{
    NSLog(@"My delegate is not nil");
    [self.delegate searchPressed];
}

这将告诉您您的委托是否设置正确。一个很大的可能性是您的委托是 nil,但是如果您在 nil 对象上调用选择器,它不会崩溃但不会做任何事情。

于 2013-02-06T16:52:45.237 回答
1

我不确定你是如何设置的,但这对我有用。在 CategoryFilterView.xib 中,我用一个按钮创建了一个视图,该按钮连接到 CategoryFilterView.m 文件中的 IBAction searchPressAction:。我删除了该文件中的初始化代码(因此唯一的代码是该操作方法)。我在 CategoryFilterView xib 中设置 MainViewController 文件的所有者。然后在 MainViewController 中,我有这段代码来添加子视图:

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    self.subviewArray = [[NSBundle mainBundle] loadNibNamed:@"CategoryFilterView" owner:self options:nil];
    UIView *mainView = [self.subviewArray objectAtIndex:0];
    CategoryFilterView *temp = (CategoryFilterView *)mainView;
    temp.delegate = self;
    self.switchViewOutlet = temp;
    [self.view addSubview:self.switchViewOutlet];
}

您必须向 searchBarCancelButtonClicked: 方法添加类似的代码才能再次获取视图。

编辑后:

我不清楚您是否需要使用委托。如果您使 MainViewController 成为 nib 中视图的文件所有者,那么您可以将该视图中的任何按钮直接连接到 MainViewController.m 中的 IBActions,而无需使用委托。在这种情况下,您也不需要对该视图进行子类化——它可以只是一个 UIView。

于 2013-02-06T18:01:22.337 回答