自定义委托方法用法

时间:2015-11-10 21:44:53

标签: objective-c

我正在尝试在NSObject(MenuComponent)和UIViewController(fullVC)类之间使用自定义委托。委托在UIViewController中声明,因为UIViewController需要将数据(标题和url)发送到我的NSObject类。

我的协议方法在UIViewController(FullVC)

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

我正在尝试在My FullVC中调用委托方法。 m文件

FullVC.m

@interface FullVC () 

@end
@implementation FullVC

 - (void)viewDidLoad
 {
     [super viewDidLoad];
 }

 -(void)scrollViewInit
 {  
    _title= @“Title of a Link”;

    _urlString = @“Link of the Url”;

        if ([delegate     respondsToSelector:@selector(sharetoView:inUrl:)])

         {

                NSLog(@“Checking urlString in scrollView method: %@", _urlString);

                 [self.delegate shareToView:_title inUrl:_urlString];

             }

      } 

我的shareToView方法在我的NSObject类

中实现

MenuComponent.m

 @interface MenuComponent() <MenuComponentDelegate>

 @property (nonatomic, weak) NSString *titleToShare;
 @property (nonatomic, weak) NSString *urlToShare;

 @end 

@implementation MenuComponent

      -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
     {

       if ([self.nav_controller.topViewController isKindOfClass:[FullVC class]])
        {
             if (indexPath.row==0)
             {
                  [(FullVC *)self.vav_controller.topViewController setDelegate:self];
           NSLog(@“The value of title to share: %@“,titleToShare);

              }
          }

      }
  -(void)shareToView:(NSString *)titleString inUrl:(NSString *)urlString
     {
         NSLog(@"title string after passing is: %@", titleString);
         NSLog(@"url string after passing is: %@", urlString);
     }
        @end

我在委派数据时遇到问题。我的shareToView方法似乎超出了范围。我的NSLog不是打印。唯一的NSLog打印是显示titleToShare的价值的打印。但是这个值打印为Null。

要分享的标题值:( null)

是否有人可以帮助我如何调用委托方法,因为看起来控制器没有调用并且不确定它是否在范围内?谢谢

0 个答案:

没有答案