分段控制总是返回0
我正在使用 TabBar 应用程序和包含 UISegmentedControl
的导航项。
当捕获事件“值更改”时,我已经连接了一个方法。
该方法总是捕获 0 作为 SegmentIndex...
这是我的头文件:
#import <UIKit/UIKit.h>
@interface GraphNavController : UINavigationController {
IBOutlet UIImage *image;
CGPoint gestureStartPoint;
UISegmentedControl *segmentedControl;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
-(IBAction) segmentedControlIndexChanged;
-(void)journalier;
-(void)mensuel;
-(void)annuel;
@property (nonatomic, retain) IBOutlet UIImage *image;
@property (nonatomic, retain) IBOutlet UISegmentedControl *segmentedControl;
@end
该方法在这里:
-(IBAction) segmentedControlIndexChanged{
switch (self.segmentedControl.selectedSegmentIndex) {
case 0:
NSLog(@"1");
break;
case 1:
NSLog(@"2");
break;
case 2:
NSLog(@"3");
break;
default:
break;
}
}
我希望我们能找到解决方案
非常感谢
I'm using a TabBar app with a navigation item that includes a UISegmentedControl
.
I've connected a method when the event "value changed" is caught.
The method always catch 0 as SegmentIndex...
Here's my header file :
#import <UIKit/UIKit.h>
@interface GraphNavController : UINavigationController {
IBOutlet UIImage *image;
CGPoint gestureStartPoint;
UISegmentedControl *segmentedControl;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
-(IBAction) segmentedControlIndexChanged;
-(void)journalier;
-(void)mensuel;
-(void)annuel;
@property (nonatomic, retain) IBOutlet UIImage *image;
@property (nonatomic, retain) IBOutlet UISegmentedControl *segmentedControl;
@end
The method is here :
-(IBAction) segmentedControlIndexChanged{
switch (self.segmentedControl.selectedSegmentIndex) {
case 0:
NSLog(@"1");
break;
case 1:
NSLog(@"2");
break;
case 2:
NSLog(@"3");
break;
default:
break;
}
}
I hope we will find a solution
Thanks a lot
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
此问题的一个可能的解释是 self.segmentedControl 为零。 self.segmentedControl 是 IBOutlet 吗?还是用代码创建的?检查 self.segmentedControl == nil 是否。
A possible explanation for this problem is that self.segmentedControl is nil. Is self.segmentedControl as an IBOutlet? Or created in code? Check if self.segmentedControl == nil.
我遇到了同样的问题,并与 nill UISegmentedControl 的错误作斗争。
我找到了答案。
有时您会遇到同样的问题,但您在 XIB 中拥有所有连接。
问题: self.segmentedControl 中没有任何内容。
在大多数情况下,当 XIB 中的所有连接都正确时,它也将无法工作。
原因:由于研究 Objective-C 的差异,我们使用不同的代码编写风格。
决策:例如我们在接口UISegmentedControl *control;中编写
当你@synthesize时你会怎么做?
如果你写@synthesize control=_control;,那么当你这样写NSLog(@"%i",control.selectedSegmentIndex)时,你将永远为零。
所以我们在 @synthesize 或代码中出现了错误。
决定
对于那些编写@synthesize控件的人;使用control.selectedSegmentIndex
对于你写@synthesize control=_control;使用 _control.selectedSegmentIndex
一切都会正常工作。
I had the same problem and fight with the bug of nill UISegmentedControl.
AND I FOUND THE ANSWER.
Sometimes you have the same problem but you have all connections in XIB.
THE PROBLEM: You have nill in self.segmentedControl.
In most situations when you have everything connected in XIB correct it will also not work.
THE CAUSE: Because of difference in studing the Objective-C we use different styles of writing code.
The Decision: For example we write in interface UISegmentedControl *control;
When you @synthesize how would you do that?
If you write @synthesize control=_control; you will always have nill when you write like that NSLog(@"%i",control.selectedSegmentIndex).
So we have the mistake in @synthesize or in Code.
Decision
For thou who write @synthesize control; use control.selectedSegmentIndex
For thou who write @synthesize control=_control; use _control.selectedSegmentIndex
And everything will work correct.