【问题标题】:How to change value in UISegmentedControl?如何更改 UISegmentedControl 中的值?
【发布时间】:2012-08-11 03:07:54
【问题描述】:

我在我的项目中设置了分段控件。我想在我的设置场景中使用它来控制方向是横向左侧还是横向右侧。我已经在我的情节提要中进行了设置,这是我的 SettingsViewController.h 中的代码

#import <UIKit/UIKit.h>

@interface SettingsViewController : UIViewController
{
    IBOutlet UISegmentedControl *orientation;
}

@property (nonatomic, retain) UISegmentedControl *orientation;

- (IBAction) setOrientation;

@end

这是我在 SettingsViewController.m 中的代码

#import "SettingsViewController.h"

@implementation SettingsViewController

@synthesize orientation;


- (IBAction)setOrientation
{
    NSLog(@"index = %d\n", orientation.selectedSegmentIndex);
    if (orientation.selectedSegmentIndex == 0) {
        NSLog(@"left\n");
    }
    if (orientation.selectedSegmentIndex == 1) {
        NSLog(@"right\n");
    }

}  



- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad 
{
    [super viewDidLoad];

}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

@end

我想知道为什么当我选择控件的右侧时值没有从 0 变为 1。另外我想知道在我的 .h 中是否需要声明 IBOutlet 因为我不使用分段控件作为出口。我只是使用它来获取选择了哪一侧,然后使用它来设置应用程序的方向。

【问题讨论】:

    标签: xcode xcode4.3 uisegmentedcontrol


    【解决方案1】:

    试试这个。使用 valueChanged 在 Storyboard 中将其连接到 segmentedControl。

    - (IBAction)segmentedControlChanged:(id)sender {
        UISegmentedControl *segmentedC = (UISegmentedControl *)sender;
    
        if (segmentedC.selectedSegmentIndex == 0) {
    //      NSLog(@"selectedSegmentIndex == 0");
            [self selectFirstSegment];
        }
    }
    

    【讨论】:

    • 谢谢!它有效:)(我会投赞成票,但我没有足够高的代表来做到这一点:/)
    【解决方案2】:

    在segment action中设置方向横向或potriat..并在.m文件末尾设置bool方向类型方法

    return yes; 
    

    在所有 .m 文件中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-01
      • 2014-06-12
      • 2012-02-20
      • 2020-01-06
      • 2016-03-18
      • 2022-01-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多