Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it Possible to Change Action For UIButton?

I am trying to change Action For the UIButton in ios applicatio. I did Following Code

 button = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    [button addTarget:self  action:@selector(aMethodShow:) forControlEvents:UIControlEventTouchDown];

    [button setTitle:@"Show View" forState:UIControlStateNormal];

    button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);

    [view addSubview:button];

In particular Section I want to change Action for this Button .So i did this

[button addTarget:self  action:@selector(aMethodHide:) forControlEvents:UIControlEventTouchDown];
[button setTitle:@"Hide View" forState:UIControlStateNormal];

Unfortunately This code note Working?

like image 220
Musthafa P P Avatar asked Jan 09 '12 06:01

Musthafa P P


Video Answer


2 Answers

I suggest before adding new target, first invoke removeTarget on UIbutton object then add new target with other action.

like image 170
Kumaran Avatar answered Sep 30 '22 03:09

Kumaran


I think it will help you

[yourButton removeTarget:nil 
               action:NULL 
     forControlEvents:UIControlEventAllEvents]; 

[yourButton addTarget:self 
               action:@selector(yourAction:)
     forControlEvents:UIControlEventTouchUpInside];
like image 32
Atanu Mondal Avatar answered Sep 30 '22 03:09

Atanu Mondal