gpt4 book ai didi

ios - 如何从 Iphone 中的另一个 uiview 调用协议(protocol)?

转载 作者:行者123 更新时间:2023-12-01 18:15:07 24 4
gpt4 key购买 nike

My Question is similar to this Previously ask question please provide me some solution for this ??

在我的 SettingView.h

@protocol settingViewProtocol <NSObject>
-(void)getSelectedLanguageID:(NSInteger)langID;
@end

@interface SettingView : UIView<UITableViewDataSource,UITableViewDelegate,UIPickerViewDelegate,UIPickerViewDataSource>
{

NSMutableArray *settingElementArray;
NSMutableArray *settingImageArray;
UIPickerView *LanguagePickerView;
NSArray *pickerArray;
UIToolbar *toolBar;
}

@property (assign)id<settingViewProtocol> delegate;

// in my SettingView.m

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
[self.delegate getSelectedLanguageID:row];
}

//在我的 GridView.h
#import "SettingView.h"
@interface GrideView : UIView <AVAudioPlayerDelegate,settingViewProtocol>{

MoreOptionView *moreOption;
AVAudioPlayer *audioPlayer;
BOOL isPlaying;
SettingView *settingView;

}
-(void)awakeFromNib
{
[super awakeFromNib];
settingView = [[SettingView alloc]init];
settingView.delegate = self;
}

但是委托(delegate)返回零值??

编辑代码以获取更多信息

//在我的 View Controller 中
self.settingView = (SettingView *)[[[NSBundle mainBundle]loadNibNamed:NSStringFromClass([SettingView class]) owner:self options:nil]objectAtIndex:0];
[self.settingView setFrame:CGRectMake(320*0, 0,bottomScrollView.frame.size.width, bottomScrollView.frame.size.height)];
self.settingView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"settingbackground.png"]];
[self.bottomScrollView addSubview:self.settingView];

提前致谢

最佳答案

原因是因为delegate属性为 nil - 您实例化两个不同的对象。

您在这里创建一个对象:

@interface GrideView : UIView <AVAudioPlayerDelegate,settingViewProtocol>

-(void)awakeFromNib
{
[super awakeFromNib];
settingView = [[SettingView alloc]init];
settingView.delegate = self;
}

你创建它并设置 delegate .并且这个对象在 delegate 中有值属性(property)。

您还可以创建 另一个 SettingView 的实例这里:
self.settingView = (SettingView *)[[[NSBundle mainBundle]loadNibNamed:NSStringFromClass([SettingView class]) owner:self options:nil]objectAtIndex:0];
[self.settingView setFrame:CGRectMake(320*0, 0,bottomScrollView.frame.size.width, bottomScrollView.frame.size.height)];
self.settingView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"settingbackground.png"]];
[self.bottomScrollView addSubview:self.settingView];
SettingView 的这个实例 不是 delegate 中有值属性(property)。

你怎么能解决?您需要使用 SettingView 的所有实例在你查看 Controller 。例如,如果您拥有 GrideView 的属性(property)在您的 View Controller 中:
@interface ViewController
...
@property (nonatomic, weak) IBOutlet GrideView *gridView;
...
@end

您可以创建 SettingView-awakeFromNibGrideView并为 View Controller 公开属性:
@implementation GrideView 
...
- (void)awakeFromNib
{
[super awakeFromNib];
self.settingView = (SettingView *)[[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([SettingView class]) owner:self options:nil] objectAtIndex:0];
self.settingView.delegate = self;
}
...
@end

interface 中使用它创建属性部分:
@interface GridView : UIView
...
@property (nonatomic, strong) SettingView *settingView; // strong - because you want to keep it till view controller use it
...
@end

而你在 View Controller 中作为 gridView 的属性:
[self.gridView.settingView setFrame:CGRectMake(320*0, 0,bottomScrollView.frame.size.width, bottomScrollView.frame.size.height)];
self.gridView.settingView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"settingbackground.png"]];
[self.bottomScrollView addSubview:self.gridView.settingView];

关于ios - 如何从 Iphone 中的另一个 uiview 调用协议(protocol)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22728113/

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