简体   繁体   中英

Swift can not see objc class function using block

I am using HMSegmentedControl( https://github.com/HeshamMegid/HMSegmentedControl ) in swift.

When I tried to use the following function

- (void)setIndexChangeBlock:(IndexChangeBlock)indexChangeBlock;

Xcode could not find his function when I used the following syntax

categoriesView.setIndexChangeBlock {

}

It says "HMSegmentedControl does not have a member named categoriesView.setIndexChangeBlock. I noticed another function in HMSegmentedControl that does not use block has been recognized by Xcode.

- (void)setSelectedSegmentIndex:(NSUInteger)index animated:(BOOL)animated;

How to fix it?

Thanks!

Set block using this

categoriesView.indexChangeBlock = { (index) in

  if index == 0 {
  //Do something
  }
}

The setIndexChangeBlock: method expects a block parameter IndexChangeBlock , which is defined as follows:

typedef void (^IndexChangeBlock)(NSInteger index);

This means it expects an NSInteger parameter. You would need to call the method as follows:

categoriesView.setIndexChangeBlock { (i: Int) -> () in

}

You can set visible scrollview area like:

func segmentedControlChangedValue(segmentedControl: HMSegmentedControl) {
        self.scrollView.scrollRectToVisible(CGRect.init(x: UIScreen.main.bounds.size.width * CGFloat(segmentedControl.selectedSegmentIndex), y: (self.scrollView.frame.origin.y), width: UIScreen.main.bounds.size.width, height: (self.scrollView.frame.size.height)), animated: true)

}

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM