【问题标题】:How to know UISwitch button clicked如何知道 UISwitch 按钮被点击
【发布时间】:2011-07-19 15:38:10
【问题描述】:

我有一个包含自定义视图界面的故事。这是customeview类定义。

@interface CustomTableCellview : UITableViewCell {

UILabel *titleOfPost;
UILabel *userProfileName;
UIImageView* profileImage;
UILabel *countOfFave;



}

 @property(nonatomic,retain) IBOutlet UIImageView *profileImage;
 @property(nonatomic,retain) IBOutlet UILabel *titleOfPost;
  @property(nonatomic,retain) IBOutlet UILabel *countOfFave;
 @property(nonatomic,retain) IBOutlet UILabel *userProfileName;

这些所有值都显示在我的表格中。每当用户单击开/关按钮时,我都想调用一个函数。这是我的 tableview 类定义。

 @interface VIPsScreen : UITableViewController {
 NSMutableArray* tableList;

IBOutlet CustomVIPCell *tblCell;

NSMutableArray* chengdArray;

}

我想将所有相关的“userProfileName”存储在 UISWitch 值为“ON”的数组“chengdArray”中。我将如何解决这个问题。 提前致谢

【问题讨论】:

    标签: iphone uiswitch


    【解决方案1】:

    使用

    [switchCtl addTarget:self action:@selector(action:) forControlEvents:UIControlEventValueChanged];
    

    你在哪里初始化 UISwitch 的实例和

    -(void)action:(id)sender{
    
    }
    

    作为一种回调方法,您应该在其中检查是否已切换并采取措施...

    【讨论】:

    • 是的,我现在正在调用函数。但是我怎么知道它在哪一行被点击了。我是这样添加的。 cell.userName.text=[profileUserName objectAtIndex:indexPath.row]; cell.userProfileImage.image=[UIImage imageNamed:[imageData objectAtIndex:indexPath.row]]; [cell.VIPsYEsNo addTarget:self action:@selector(action:) forControlEvents:UIControlEventValueChanged];我可以在函数“action”中传递参数吗?
    • 最简单的方法是使用VIPsYEsNo的tag属性。假设您可以将其设置为行索引或与您的数据集相关的任何其他索引。在-(void)action:(id)sender;中,发送者实际上是一个被触发的UISwitch,所以你可以从中取回标签值。
    • 它工作得很好:)。请帮助如何知道 UISwitch 按钮是 ON 还是 OFF 检测。
    • UISwitch 中有一个属性声明为:@property(nonatomic, getter=isOn) BOOL on 所以你可以写VIPsYEsNo.on
    【解决方案2】:

    您可以将此 [tempSwitch isOn] 与 if 语句一起使用,其中 tempSwitch 是您的 switchView 的名称

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-31
      • 1970-01-01
      相关资源
      最近更新 更多