我在 ARC 中使用自定义委托类时遇到了一个奇怪的问题。在 viewDidLoad 之后的视图控制器中,我调用工具栏的 setDelegate 方法并将视图控制器分配为委托。
每当我尝试引用委托时,它为零,即使在视图控制器中设置后也是如此。
//MPToolbar.h
#import <UIKit/UIKit.h>
//Define the protocol for the delegate
@class MPToolBar;
@protocol MPToolBarDelegate <NSObject>
@required
- (void)writeButtonSelected;
- (void)writeButtonDeSelected;
@end
@interface MPToolBar : UIView{
id <MPToolBarDelegate> delegate;
}
@property(strong) UIButton *lastButton;
@property(strong) id <MPToolBarDelegate> delegate;
-(IBAction)buttonSelected:(id)sender;
-(void)resizeForOrientation:(UIInterfaceOrientation)orientation;
@end
//MPToolbar.m
#import "MPToolBar.h"
@implementation MPToolBar
@synthesize lastButton, delegate;
- (id)initWithFrame:(CGRect)frame
{
if ((self = [super initWithFrame:frame])) {
// Initialization code
}
NSArray *nibViews = [[NSBundle mainBundle] loadNibNamed:@"MPToolBar" owner:self options:nil];
[self addSubview:[nibViews objectAtIndex:0]];
return self;
}
#pragma button actions
-(IBAction)buttonSelected:(id)sender{
UIButton *btn = (UIButton *)sender;
if(lastButton && lastButton != btn)[self deselect:lastButton];
if (btn.selected){
[self deselect:btn];
return;
}
[btn setSelected:YES];
lastButton = btn;
switch (btn.tag) {
case 0:
[self.delegate writeButtonSelected];
break;
}
}
-(void)deselect:(UIButton *)btn{
[btn setSelected:NO];
switch (btn.tag) {
case 0:
[self.delegate writeButtonDeSelected];
break;
}
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
-(void)resizeForOrientation:(UIInterfaceOrientation)orientation{
if (UIInterfaceOrientationIsLandscape(orientation)) {
self.frame = CGRectMake(0, 44, 1024, 60);
}
if (UIInterfaceOrientationIsPortrait(orientation)) {
self.frame = CGRectMake(0, 44, 768, 60);
}
}
@end
//ViewTest.h
#import <UIKit/UIKit.h>
#import "MPToolBar.h"
@interface ViewTest : UIViewController <MPToolBarDelegate>
{
MPToolBar *toolBar;
}
@property(strong) MPToolBar *toolBar;
@end
//ViewTest.m
#import "ViewTest.h"
@interface ViewTest ()
@end
@implementation ViewTest
@synthesize toolBar;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.toolBar = [[MPToolBar alloc] initWithFrame:CGRectMake(0, 44, 1024, 60)];
[self.view addSubview:self.toolBar];
[self.toolBar setAlpha:1.0];
[self.toolBar setDelegate:self];
// Do any additional setup after loading the view from its nib.
}
#pragma mark - MPToolBar Delegate Methods
-(void)writeButtonSelected{
//set new annotation for write mode and size to screen bounds.
NSLog(@"writeButtonSelected");
}
- (void)writeButtonDeSelected{
NSLog(@"writeButtonDeSelected");
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
@end