【问题标题】:Updating one UISegmentedControl with the value of another UISegmentedControl用另一个 UISegmentedControl 的值更新一个 UISegmentedControl
【发布时间】:2014-01-13 04:45:50
【问题描述】:

我想单击一个按钮并使一个 UISegmentedControl 的值进入第二个相同的 UISegmented 控件。

这是我尝试过的代码,它什么都不做:

    _stopFractionControl = _startFractionControl;

提前致谢!

【问题讨论】:

    标签: ios objective-c uisegmentedcontrol


    【解决方案1】:

    真的很简单:

    1. 为段控件创建IBAction
    2. 其余的都在行动中

    -(IBAction)segmentAction:(sender)
    {
        segmentedControl.selectedSegmentIndex=segmentedControl2.selectedSegmentIndex;
        [segmentedControl1 sendActionsForControlEvents:UIControlEventValueChanged];
    }
    

    【讨论】:

    • 感谢您的回复。我不希望这与 UISegmentedControl 操作相关联。我希望这个结果与另一个 UIButton 点击​​相关联。有什么想法吗?
    • 这在 UIButton 中不起作用:` stopFractionControl.selectedSegmentIndex=startFractionControl.selectedSegmentIndex; [stopFractionControl sendActionsForControlEvents:UIControlEventValueChanged];`
    • 我无法正确理解您......如果您希望在按钮操作中完成此操作,则只需在按钮操作方法中添加相同的代码。这将使第二段控制复制第一段控制的状态。
    【解决方案2】:

    您正试图将一个完整对象设置为另一个对象,但您需要做的就是更改第一个对象的参数与另一个对象的参数的关系。
    因此,您需要更改的是 selectedSegmentIndex 对象的 selectedSegmentIndex 参数:

    试试:

    //get segment index of a UISegmentedController object
    NSInteger otherSegmentIndex = _startFractionControl.selectedSegmentIndex
    
    //set segment index of a UISegmentedController object to the above
    [_stopFractionControl setSelectedSegmentIndex: otherSegmentIndex];
    
    //implement the change
    [_stopFractionControl sendActionsForControlEvents:UIControlEventValueChanged];
    

    【讨论】:

    • 我觉得这是正确的方向,但我掌握了一些错误。我会和它一起玩。 Implicit conversion of 'NSInteger' (aka 'int') to 'NSNumber *' is disallowed with ARC
    • 又一击。 Incompatible integer to pointer conversion initializing 'NSNumber *__strong' with an expression of type 'NSInteger' (aka 'int')
    • 最后一个。 Incompatible pointer to integer conversion sending 'NSNumber *__strong' to parameter of type 'NSInteger' (aka 'int')
    • 我得到了基于以下代码的先前错误:NSNumber *otherSegmentIndex = _startFractionControl.selectedSegmentIndex; //set segment index of a UISegmentedController object to the above [_stopFractionControl setSelectedSegmentIndex: otherSegmentIndex]; //implement the change [_stopFractionControl sendActionsForControlEvents:UIControlEventValueChanged];
    • @C_Dub :(我的和 @AnubrataSantra 的答案几乎相同)无论如何...更新了答案。它应该是NSInteger。顺便说一句... -sendActionsForControlEvents 是必需的,否则它只是一个视觉变化
    猜你喜欢
    • 1970-01-01
    • 2012-06-18
    • 2011-04-26
    • 1970-01-01
    • 1970-01-01
    • 2013-09-06
    • 1970-01-01
    • 2011-01-31
    • 1970-01-01
    相关资源
    最近更新 更多