【问题标题】:UISegmentedControl change event not firing in iOS5UISegmentedControl 更改事件未在 iOS5 中触发
【发布时间】:2012-10-16 19:26:51
【问题描述】:

我有一个 UISegmentedControl,它的“Value changed”事件在 Interface Builder 中被连接起来,以调用我的控制器的 -(IBAction)segmentChangeAction:(id)sender;

当用户点击控件以更改所选段时,无论在 iOS4 还是 iOS5 中,都会按预期调用 segmentChangeAction

当我通过segmentedControl.selectedSegmentIndex = newIndex; 以编程方式更改所选段时,在iOS4 上调用segmentChangeAction 并且该段反映了新的选择。然而在 iOS5 segmentChangeAction没有被调用,但该段确实反映了新的选择。

这是 iOS5 的变化吗?当我以编程方式更改选择时,我可以做些什么来在 iOS5 上调用 segmentChangeAction

【问题讨论】:

    标签: iphone ios ios5 uisegmentedcontrol


    【解决方案1】:

    这是 iOS 5 中的一项更改,以使 UISegmentedControl 与所有其他控件保持一致。

    这个想法是动作应该只作为用户交互的结果自动触发。在 iOS 5 之前,UISegmentedControl 的操作会因为用户交互编程交互而被触发。但是,以编程方式启动更改意味着您也可以自己执行[myControl sendActionsForControlEvents:UIControlEventValueChanged]

    但是,您必须小心这一点。说你这样做:

    [segmentedControl setSelectedSegmentIndex:newIndex];
    [segmentedControl sendActionsForControlEvents:UIControlEventValueChanged];
    

    如果您在 iOS 5 上构建并运行它,它会按预期工作。如果您在 iOS 4 上构建并运行它,您的操作将触发两次(一次是 setSelectedSegmentIndex,另一次是 sendActions...)。

    解决这个问题的方法是进行某种保护。这可能是运行时检查,表明您在 iOS 5+ 设备上运行,或者甚至可能是更普通的东西,如下所示:

    // changingIndex is a BOOL ivar
    changingIndex = YES;
    [segmentedControl setSelectedSegmentIndex:newIndex];
    changingIndex = NO;
    [segmentedControl sendActionsForControlEvents:UIControlEventValueChanged];
    

    然后在您的操作方法中...

    - (void)segmentedControlSelectedIndexChanged:(id)sender {
      if (!changingIndex) {
        // your action code here, guaranteed to only run as a result of the sendActions... msg
      }
    }
    

    【讨论】:

    • 感谢您的深入解释!我做了最简单的事情(更改索引后直接调用segmentChangeAction),验证了在iOS
    • @Vin 今年不行;我太忙了,无法给予它作为版主应得的关注。
    • @Clafou 不客气!如果您发现UIControl 遵循此行为,请file a bug,它会得到修复。 :)
    • @Dave 太糟糕了...看起来今年我们也不会有来自 iOS 或目标 c 的任何人:(
    • @Vin Brad Larson、BoltClock 和 Moshe 已被提名,并且都活跃在 Cocoa 相关标签中。
    【解决方案2】:

    我找到了另一种方式,可能更容易理解 您可以扩展 UISegmentedControl 并在 init 方法中添加目标操作并调用委托方法来触发值更改

    这里是示例代码

    头文件是这样的

    #import <UIKit/UIKit.h>
    @class CGUISegmentedControl;
    
    @protocol CGUISegmentedControlDelegate <NSObject>
    
    @optional
    - (void) segmentedControl:(CGUISegmentedControl *) control valueChangedTo:(NSInteger) nValue;
    
    @end
    
    @interface CGUISegmentedControl : UISegmentedControl
    
    @property (nonatomic,unsafe_unretained) id <CGUISegmentedControlDelegate> delegate;
    
    @end
    

    .m 文件

        #import "CGUISegmentedControl.h"
    
    @implementation CGUISegmentedControl
    
    @synthesize delegate                = _delegateAction;
    
    - (void) addTargetAction {
    
        [self addTarget:self action:@selector(indexChanged:) forControlEvents:UIControlEventValueChanged];
    
    }
    
    - (id)initWithFrame:(CGRect)frame
    {
        self = [super initWithFrame:frame];
        if (self) {
            [self addTargetAction];
        }
        return self;
    }
    
    - (id) initWithCoder:(NSCoder *)aDecoder {
    
        self = [super initWithCoder:aDecoder];
        if (self) {
            [self addTargetAction];
        }
        return self;
    
    }
    
    - (id) initWithItems:(NSArray *)items {
    
        self = [super initWithItems:items];
       if (self) {
            [self addTargetAction];
        }
        return self;
    }
    
    - (id) init {
    
        self = [super init];
        if (self) {
            [self addTargetAction];
        }
        return self;
    
    }
    
    - (void) indexChanged:(id) sender {
    
        if (_delegateAction && [_delegateAction respondsToSelector:@selector(segmentedControl:valueChangedTo:)])
            [_delegateAction segmentedControl:self valueChangedTo:self.selectedSegmentIndex];
    
    
    }
    
    @end
    

    你可以在调用类中设置委托

    【讨论】:

      猜你喜欢
      • 2012-03-10
      • 1970-01-01
      • 1970-01-01
      • 2013-05-26
      • 2021-08-24
      • 2021-09-30
      • 2014-01-03
      • 1970-01-01
      相关资源
      最近更新 更多