gpt4 book ai didi

从 viewController 调用 setDelegate 后 iOS 自定义委托(delegate)返回 Nil

转载 作者:行者123 更新时间:2023-12-01 17:44:01 25 4
gpt4 key购买 nike

我在 ARC 中使用自定义委托(delegate)类时遇到了一个奇怪的问题。在 viewDidLoad 之后的 View Controller 中,我调用工具栏的 setDelegate 方法并将 View Controller 分配为委托(delegate)。

每当我尝试引用委托(delegate)时,它是零,即使在被设置后
View Controller 。

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

最佳答案

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

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

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

关于从 viewController 调用 setDelegate 后 iOS 自定义委托(delegate)返回 Nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11264797/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com