问题
I am new to IOS, and Trying to pass two values form a UIViewController to a UIView. I followed few examples,some posts, tried and still no luck. I am getting this error while running the application. The error is ..
[MenuComponent setDelegate:]: unrecognized selector sent to instance
MenuComent is My UIView. FullVC is my UIViewController.
FullVC.h
@class FullVC;
@protocol MenuComponentDelegate <NSObject>
-(void)shareToView:(NSString *)titleString inUrl:(NSString *)urlString;
@end
@interface FullVC:UIViewController<UIScrollViewDelegate,UITextViewDelegate>
@property (nonatomic,assign) id delegate;
@end
FullVC.m
@interface FullVC ()
@end
@implementation FullVC
@synthesize delegate;
- (void)viewDidLoad
{
[super viewDidLoad];
}
-(void)scrollViewInit
{
[self.delegate shareToView:title.text inUrl:urlString];
}
@end
MenuComponent.m
@interface MenuComponent()
@end @implementation MenuComponent
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([self.nav_controller.topViewController isKindOfClass:[FullVC class]])
{
if (indexPath.row==0)
{
[(FullVC *)self setDelegate:self];
}
}
}
-(void)shareToView:(NSString *)titleString inUrl:(NSString *)urlString
{
NSLog(@"title string after passing is: %@", titleString);
NSLog(@"url string after passing is: %@", urlString);
}
@end
I am getting error at this statement.
[(FullVC *)self setDelegate:self];
can somebody please help.
Thanks
回答1:
[(FullVC *)self setDelegate:self];
I think in this line self is the instance of MenuComponent class, you can not simply change the type of self to FullVC, it is wrong.
I'm not reading all of your code,or you can try this
[(FullVC *)self.nav_controller.topViewController setDelegate:self];
also in FullVC:
-(void)scrollViewInit
{
[self.delegate shareToView:title.text inUrl:urlString];
// you can simply check this delegate respond shareToView:inUrl: or not
/**
* if ([_delegate respondsToSelector:@selector(shareToView:inUrl:)])
* {
* [_delegate shareToView:title.text inUrl:urlString];
* }
*/
}
来源:https://stackoverflow.com/questions/33605700/uiview-setdelegate-unrecognized-selector-sent-to-instance