开发者

UISegmentedControl and adding targets

开发者 https://www.devze.com 2023-03-30 06:11 出处:网络
I\'m sure this is an easy one for someone out there. I have a UISegmentedControl which I am using as a button (so as not to have to use the nasty default button) and I am having trouble getting the ta

I'm sure this is an easy one for someone out there. I have a UISegmentedControl which I am using as a button (so as not to have to use the nasty default button) and I am having trouble getting the target to work....code as follows

- (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];



}

and then

-(void)changeFilter:(id)sender{

}

for some reason, clicking on the UISegmentedControl does not fire the target method.

As an addendum, is there a simpler way to make nice looking UIButtons? I don't have access to Photoshop at work (although I do have gimp installed), so a way which doesn't involve image making would be good. I can't believe that apple didn't supply nice looking UIButtons in the UI, it seems such a fundamental thing to need?

Anyway, thanks for the help mis amigos.

thanks for the answers...I've tried the fix but it still doesn't fire

I now have

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

then

@interface
-(voi开发者_StackOverflowd)changeFilter:(id)sender;

and

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

Note that the method is in the same class as the UISegmentedControl. Maybe I should just try using the Glass button API which was suggested, but my boss hates me using third party libraries if there is a way of avoiding it!


The selector is wrong. It should be, action:@selector(changeFilter:), And change the event to forControlEvents:UIControlEventValueChanged, as UISegmentedControl doesn't send any actions for UIControlEventTouchUpInside event.


.h file u forgot to put this

     -(void)changeFilter:(id)sender;

then

change this 

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

To

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


The action will automatically send the sender so just try @selector(changeFilter:).

As for your second question, ios5 will help you out there... but I can't say much about that yet. For now, you can use some uibutton classes that people have made to easily make some good looking buttons. Here's one:

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

0

精彩评论

暂无评论...
验证码 换一张
取 消