在集合视图中为 UIswitch 编写事件 [英] writing events for UIswitch in collection view

查看:11
本文介绍了在集合视图中为 UIswitch 编写事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我正在尝试在 UI 集合视图中为 UI 切换和 UI 分段控件编写事件.

Hi I am trying to write events for UI switch and UI segmentation control in UI collection view.

我在集合视图单元格中声明了 UIswitch 和 UIsegmentation 控件.

I declared the UIswitch and UIsegmentation control in collection view cell.

@interface CollectionViewCell : UICollectionViewCell

@interface CollectionViewCell : UICollectionViewCell

@property (strong, nonatomic) IBOutlet UISegmentedControl *mySegmentedControl;
@property (strong, nonatomic) IBOutlet UISwitch *Myswitch;

并从 view controller.m 访问它

and access it from view controller.m

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
 {
 CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CELL" forIndexPath:indexPath];
if(cell.Myswitch.isOn)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"!Alert"
                                                message:@"Do you want to exit the application?"
                                               delegate:self
                                      cancelButtonTitle:@"Cancel"
                                      otherButtonTitles:@"Yes", nil];
[alert show];
}
else
{

}
}

但是 UI 切换和分段控制都不适用于我.任何帮助将不胜感激.

But UI switch and segmentation control both not works for me. Any help will be appreciated.

推荐答案

  1. cellForItemAtIndexPath 方法中使用 addTarget:action:forControlEvents: 方法:
  1. Use addTarget:action:forControlEvents: in method in cellForItemAtIndexPath method:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];
    cell.mySwitch.tag = indexPath.row;
    [cell.mySwitch addTarget:self action:@selector(switchValueChanged:) forControlEvents:UIControlEventValueChanged];
    //another cell setup code
    return cell;
}

  1. 实现回调方法:

- (void) switchValueChanged: (UISwitch *) sender {
    NSInteger index = sender.tag;
    //your code
}

  1. didSelectItemAtIndexPath
  2. 中删除该代码
  1. Remove that code from didSelectItemAtIndexPath

这篇关于在集合视图中为 UIswitch 编写事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆