如何在选择某个selectedSegmentIndex时设置NSString值?

时间:2013-10-24 10:05:21

标签: ios objective-c nsstring uisegmentedcontrol

我有一个分段控件,当触摸日志时,选择了selectedSegmentIndex。但是,它为null

输出self.sessoField.text;

任何想法如何解决这个问题?

感谢

这是以下控制台输出:

2013-10-24 11:00:11.131 [3628:60b] gender is (null) 1
2013-10-24 11:00:40.179 [3628:60b] gender is (null) 0
2013-10-24 11:00:41.546 [3628:60b] gender is (null) 1

- (IBAction)genderSelectButton:(id)sender {

    if (self.segmentedControlGender.selectedSegmentIndex == 0) {
        self.sessoField.text = @"m";
        NSLog(@"gender is %@ %d",self.sessoField.text,self.segmentedControlGender.selectedSegmentIndex);

    } else if(self.segmentedControlGender.selectedSegmentIndex == 1) {
        self.sessoField.text = @"f";
        NSLog(@"gender is %@ %d",self.sessoField.text,self.segmentedControlGender.selectedSegmentIndex);

    }
}

sessoField已在.h中声明为:

@property (nonatomic, weak) IBOutlet UITextField * sessoField;

2 个答案:

答案 0 :(得分:1)

首先检查你的textfield是不是nil然后尝试这样: -

NSLog(@"gender is %@ %d",self.sessoField.text,self.segmentedControlGender.selectedSegmentIndex);

答案 1 :(得分:-1)

@property (nonatomic, weak) IBOutlet UITextField * sessoField;

should be 

@property (nonatomic, strong) IBOutlet UITextField * sessoField;