分段控制总是返回0

发布于 2024-11-03 16:34:49 字数 1138 浏览 6 评论 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

千纸鹤 2024-11-10 16:34:50

此问题的一个可能的解释是 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.

嘿嘿嘿 2024-11-10 16:34:50

我遇到了同样的问题,并与 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文