My Question is similar to this Previously ask question please provide me some solution for this ??
In my 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];
}
//In my GrideView.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;
}
but delegate returns nil value ??
Edit Code for more Info
//In my ViewController
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];
Thanks in advance
If the setting view is already in your nib, and the grid view is already in your nib, then change the delegate property declaration to an IBOutlet:
@property (assign) IBOutlet id<settingViewProtocol> delegate;
And make the delegate connection in interface builder. Remove your awakeFromNib method.
Your current code is making a new instance of the setting view and setting that view's delegate, but this isn't the view on the screen.
The reason because delegate
property is nil - you instantiate two different objects.
You create one object here:
@interface GrideView : UIView <AVAudioPlayerDelegate,settingViewProtocol>
-(void)awakeFromNib
{
[super awakeFromNib];
settingView = [[SettingView alloc]init];
settingView.delegate = self;
}
You create it and set delegate
. And this object has value in delegate
property.
Also you create another instance of SettingView
here:
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];
This instance of SettingView
does not have value in delegate
property.
How can you resolve? You need to use all instance of SettingView
over you view controller. For example if you have have property to GrideView
in your view controller:
@interface ViewController
...
@property (nonatomic, weak) IBOutlet GrideView *gridView;
...
@end
You can create SettingView
in -awakeFromNib
of GrideView
and expose property with it for 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
Create property with it in interface
section:
@interface GridView : UIView
...
@property (nonatomic, strong) SettingView *settingView; // strong - because you want to keep it till view controller use it
...
@end
And you in view controller as property of 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];
It returns nil because you did not assign any object that confirm the settingViewProtocol to the delegate property.
Either you can set it in the xib file or you manually set it up by: self.settingView.delegate = self; And make sure that the VC you are using confirm the settingViewProtocol
One more thing is about awakeFromNib. Perhaps the time you assign the self to the delegate, you gridView is not instantiated. It's just simply gotten awake from xib file. So, check whether the self is initialized.
In setting.h file
@protocol settingViewProtocol <NSObject>
-(void)getSelectedLanguageID:(NSInteger)langID; @end
@interface SettingView : UIView {
NSMutableArray *settingElementArray; NSMutableArray *settingImageArray; UIPickerView *LanguagePickerView; NSArray *pickerArray; UIToolbar *toolBar; id delegate; }
@property (nonatomic,stronh)id delegate;
// in my SettingView.m
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
if([self.delegate respondsToSelector:@selector(getSelectedLanguageID:)]) //checking is delegate [self.delegate getSelectedLanguageID:row];
}
And now in your controller
-(void)awakeFromNib { [super awakeFromNib]; if(!settingView) settingView = [[SettingView alloc]init]; settingView.delegate = self; [self.view addsubview:settingView]; }
//then you can access -(void)getSelectedLanguageID:(NSInteger)langID{} //in your controller file
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.