UISegmentedControl以编程方式更改selectedSegmentIndex时调用action方法 [英] UISegmentedControl calls action method on changing selectedSegmentIndex programmatically

查看:129
本文介绍了UISegmentedControl以编程方式更改selectedSegmentIndex时调用action方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的xib文件中有一个UISegmentedControl。它链接到xib文件中值更改事件的操作方法。

I have a a UISegmentedControl in my xib file. It is linked to an action method on value changed event in the xib file.

当我以编程方式设置selectedSegmentIndex的值时,action方法被调用

When I programmatically set the value of selectedSegmentIndex the action method gets called

mysegmentedcontrol.selectedSegmentIndex = index

我期望action方法只会在用户通过触摸控件时调用。

I was expecting that the action method would only be called when the user changes the control by touching it?

这仅发生在UISegmentedControl。

This happens only for UISegmentedControl.

推荐答案

.h文件

BOOL isProgramaticallyChanged;

.m文件

- (IBAction)segmentAction:(id)sender { // valuechanged connected function

        UISegmentedControl *segControll = (UISegmentedControl *)sender;

    if (segControll.tag == 55) { // while create segment specify tag value to 55 (to set use via IB or Code)

        if (isProgramaticallyChanged == NO) {

            // your valuechanged code here

        }

        else {

            isProgramaticallyChanged = NO; //important

        }

    }

    else if (segControll.tag == 66) { // for many segments

    }

        //...like this do for all segments
}

在.m文件

无论您将此代码以何种编程方式更改为

wherever you put this code to change programmatically do before that like this

if (mysegmentedcontrol.selectedSegmentIndex != index) {

    isProgramaticallyChanged = YES;

    mysegmentedcontrol.selectedSegmentIndex = index;

}

这篇关于UISegmentedControl以编程方式更改selectedSegmentIndex时调用action方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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