Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UISegmentedControl and adding targets

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
-(void)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!

like image 280
shaw2thefloor Avatar asked Aug 23 '11 13:08

shaw2thefloor


3 Answers

.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];
like image 34
Vijay-Apple-Dev.blogspot.com Avatar answered Oct 30 '22 11:10

Vijay-Apple-Dev.blogspot.com


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/

like image 22
shawnwall Avatar answered Oct 30 '22 10:10

shawnwall


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.

like image 148
EmptyStack Avatar answered Oct 30 '22 10:10

EmptyStack