【问题标题】:UISegmentedControl and adding targetsUISegmentedControl 和添加目标
【发布时间】:2011-11-01 22:52:10
【问题描述】:

我相信这对外面的人来说是一件容易的事。我有一个 UISegmentedControl 用作按钮(这样就不必使用讨厌的默认按钮),我无法让目标工作....代码如下

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.

    //read.buttonType = UIBarStyleBlackOpaque;

    UISegmentedControl* read = [[[UISegmentedControl alloc] initWithFrame:CGRectMake(5, 50, 310, 54)] autorelease];
    [read insertSegmentWithTitle:@"Read" atIndex:0 animated:NO];
    read.tintColor = [UIColor colorWithRed:0.3 green:0.3 blue:0.9 alpha:1];
    read.segmentedControlStyle = UISegmentedControlStyleBar;
    [read addTarget:self action:@selector(changeFilter:sender:) forControlEvents:UIControlEventTouchUpInside];
    [read setTag:1];
    [self.view addSubview:read];



}

然后

-(void)changeFilter:(id)sender{

}

由于某种原因,单击 UISegmentedControl 不会触发目标方法。

作为附录,有没有更简单的方法来制作漂亮的 UIButtons?我在工作中无法访问 Photoshop(尽管我确实安装了 gimp),所以不涉及图像制作的方法会很好。我不敢相信苹果没有在 UI 中提供漂亮的 UIButtons,这似乎是一个需要的基本东西?

无论如何,感谢朋友们的帮助。

感谢您的回答...我已尝试修复,但仍然无法触发

我现在有

[read addTarget:self action:@selector(changeFilter:) forControlEvents:UIControlEventTouchUpInside];

然后

@interface
-(void)changeFilter:(id)sender;

@implementation
-(void)changeFilter:(id)sender{}

请注意,该方法与 UISegmentedControl 属于同一类。也许我应该尝试使用建议的 Glass 按钮 API,但是如果有办法避免的话,我的老板讨厌我使用第三方库!

【问题讨论】:

    标签: iphone objective-c ios ipad uibutton


    【解决方案1】:

    .h 文件你忘了放这个

         -(void)changeFilter:(id)sender;
    

    然后

    change this 
    
     [read addTarget:self action:@selector(changeFilter:sender:) forControlEvents:UIControlEventTouchUpInside];
    
    To
    
     [read addTarget:self action:@selector(changeFilter:) forControlEvents:UIControlEventValueChanged];
    

    【讨论】:

      【解决方案2】:

      选择器错误。应该是 action:@selector(changeFilter:),并将事件更改为 forControlEvents:UIControlEventValueChanged,因为 UISegmentedControl 不会为 UIControlEventTouchUpInside 事件发送任何操作。

      【讨论】:

      • 啊,票...感谢所有答案。
      【解决方案3】:

      该操作将自动发送发件人,因此只需尝试@selector(changeFilter:)。

      至于你的第二个问题,ios5 会帮助你...但我还不能说太多。现在,您可以使用人们制作的一些 uibutton 类来轻松制作一些好看的按钮。这是一个:

      http://hboon.com/glass-buttons-in-iphone-apps-without-using-image-files/

      【讨论】:

        猜你喜欢
        • 2018-07-26
        • 2014-01-18
        • 2015-09-08
        • 1970-01-01
        • 1970-01-01
        • 2015-12-31
        • 1970-01-01
        • 2015-08-10
        • 2012-07-06
        相关资源
        最近更新 更多