1

我在 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
4

4 回答 4

6

好的,所以我终于找到了问题所在。在我的 xib 文件中,我将 UIView 类设置为我的自定义控件类名称,而不是设置文件所有者。

一旦我将 xib 中的第一个 UIView 更改回标准 UIView,然后将文件所有者设置为我的自定义类,世界就一切正常了!

- 感谢所有试图提供帮助的人!

于 2012-07-19T04:29:04.313 回答
0

对我来说有类似的症状,我的问题是 segue 的destinationViewController 不是我期望的对象,因为它嵌入在 UINavigationController 中。

轻松修复。看这里。通过导航控制器时丢失指向委托的指针

于 2013-02-13T01:42:59.233 回答
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 开始。

于 2012-06-29T16:40:08.653 回答
0

可能导致问题的几件事:

  1. 你真的MPToolBarDelegate在它的头文件中声明你的视图控制器吗?(该代码不存在)。

  2. 这些是代码中的委托函数:

-(void)ButtonSelected;

-(void)ButtonDeSelected;

但是,您实际上是在调用这个:

[_delegate writeButtonSelected];

这不是您的协议的一部分。

于 2012-06-29T15:51:29.117 回答