search

Home  >  Q&A  >  body text

objective-c - ios addTarget参数传递问题

我写了个uiview的category,用于在控制器里添加一个可以滚到顶部的控件,

- (void)enableScrollToTopButtonForScrollView:(UIScrollView*)scrollView
{
    [scrollView addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew context:NULL];
    UIButton *scroll = self.scrollToTopButton;
    if ([scroll respondsToSelector:@selector(scrollToTop:)]) {
        return;
    }
    [scroll addTarget:self action:@selector(scrollToTop:) forControlEvents:UIControlEventTouchUpInside];
}

不知道这段代码里怎么将scrollview传递到@selector(scrollToTop:)方法里去?
让scrollview能滚动到顶部,或者除了addtarget还能有什么其他的方式来实现点击这个按钮能让scrollview做出响应,

注:代码里面的self.scrollToTopButton是通过关联模式实现的 ,默认category不能添加属性。

为情所困为情所困2762 days ago450

reply all(1)I'll reply

  • 仅有的幸福

    仅有的幸福2017-04-28 09:06:26

    "I don’t know how to pass the scrollview to the @selector(scrollToTop:) method in this code?"
    [scroll addTarget:self action:@selector(scrollToTop:) forControlEvents:UIControlEventTouchUpInside];
    The scoll has been passed in ah.
    -(void)scrollToTop:(uiscrollView *)scrollView
    {
    scrollView is the parameter you need
    }

    reply
    0
  • Cancelreply