我在ARC中使用自定义委托类时遇到了一个奇怪的问题。在viewDidLad之后的viewcontroller中,我调用工具栏setDelegate方法并将viewcontroller指定为委托。
每当我尝试引用委托时,它都是零,即使在设置之后也是如此 视图控制器。
//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
答案 0 :(得分:6)
好的,所以我终于找到了问题。在我的xib文件中,我将UIView类设置为我的自定义控件类名,而不是设置文件所有者。
一旦我将xib中的第一个UIView更改回标准的UIView,然后将文件所有者设置为我的自定义类,那么对于世界来说再次正确!
- 谢谢所有试图提供帮助的人!
答案 1 :(得分:0)
可能导致问题的几件事情:
您是否在其标题中将视图控制器声明为MPToolBarDelegate
? (该代码不存在)。
这些是代码中的委托函数:
-(void)ButtonSelected;
-(void)ButtonDeSelected;
但是,你实际上是在调用这个:
[_delegate writeButtonSelected];
这不属于您的协议。
答案 2 :(得分:0)
尝试更改您的委托声明:
@interface MPToolBar : UIView{
id delegate;
}
@property (nonatomic, assign) id <MPToolBarDelegate> delegate;
然后当你调用你的委托方法时,请使用:
[self.delegate ButtonSelected];
此外,原始iPad可升级至iOS 5.0。如果您使用的是ARC,那么无论如何都不能与4.3兼容,根据我的理解,ARC仅从5.0开始。
答案 3 :(得分:0)
对我来说有类似的症状,我的问题是segue的destinationViewController不是我期望它的对象,因为它嵌入在UINavigationController中。
轻松修复。看这里。 Lose pointer to Delegate while going through Navigation Controller