我正在尝试实现一个简单的委托,但似乎遇到了一些问题。预期的结果是用户按下UIView
链接到UIViewController
via的按钮Outlet
。我也使用UISearchBarDelegate
which 工作得很好。我只是在调用我的自定义委托方法时遇到问题。
主视图控制器.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
如果需要,我可以提供更多信息。