1

ARCでカスタムデリゲートクラスを使用すると、奇妙な問題が発生します。viewDidLoadの後のviewcontrollerで、ツールバーのsetDelegateメソッドを呼び出し、viewcontrollerをデリゲートとして割り当てます。

ビューコントローラで設定された後でも、デリゲートを参照しようとすると、nilになります。

//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

問題を引き起こしている可能性のあるいくつかの事柄:

  1. MPToolBarDelegateビューコントローラをヘッダーで実際に宣言していますか?(そのコードは存在しません)。

  2. コード内のデリゲート関数は次のとおりです。

-(void)ButtonSelected;

-(void)ButtonDeSelected;

ただし、実際にはこれを呼び出しています。

[_delegate writeButtonSelected];

これはプロトコルの一部ではありません。

于 2012-06-29T15:51:29.117 に答える
0

次のようにデリゲート宣言を変更してみてください。

@interface MPToolBar : UIView{
    id delegate;
}

@property (nonatomic, assign) id <MPToolBarDelegate> delegate;

次に、デリゲートメソッドを呼び出すときは、次を使用します。

[self.delegate ButtonSelected];

また、元のiPadはiOS5.0にアップグレード可能です。とにかく4.3と互換性のないARCを使用している場合、私が理解しているように、ARCは5.0でのみ開始されました。

于 2012-06-29T16:40:08.653 に答える
0

私にとっても同様の症状で、私の問題は、セグエのdestinationViewControllerが、UINavigationControllerに埋め込まれているため、期待していたオブジェクトではなかったことです。

簡単な修正。こちらをご覧ください。ナビゲーションコントローラを通過中にデリゲートへのポインタを失う

于 2013-02-13T01:42:59.233 に答える