objective c - Calling an IBAction in code and sending a tag through too -


so have ibaction hooked multiple buttons in ib want action fired when ibaction called want recognise int second action [sender tag].

// viewcontroller.m

-(ibaction)buttonpress {   int getinter  = [sender tag];   uiview *tmpview = [self.view viewwithtag:getinter];  nsstring *title = [(uibutton *)sender currenttitle];   } 

i have action needs call buttonpress.

 -(ibaction)secondbuttonpress {   int varint = 1   [self buttonpress: nil] <--- how pass varint buttonpress recognise sender tag here?   } 

i realise create button , duplicate code in secondbuttonpress seems messy...

 uibutton *tmpbutton2 = (uibutton *)[self.view viewwithtag:varint]; 

so question is, there way of tricking buttonpress thinking has been pressed , passing variable through sender tag action. in way programatically tricking buttonpress thinking has been pressed.

thank in advance.

uibutton *btn1 = [[uibutton alloc] initwithframe:cgrectmake(10, 10, 20, 20)]; [btn1 settitle:@"button 1" forstate:uicontrolstatenormal]; btn1.tag = 1; [btn1 addtarget:self action:@selector(buttonclick:) forcontrolevents:uicontroleventtouchupinside]; [self.view addsubview:btn1];  uibutton *btn2 = [[uibutton alloc] initwithframe:cgrectmake(10, 50, 20, 20)]; [btn2 settitle:@"button 2" forstate:uicontrolstatenormal]; btn2.tag = 2; [btn2 addtarget:self action:@selector(buttonclick:) forcontrolevents:uicontroleventtouchupinside]; [self.view addsubview:btn2];  uibutton *btn3 = [[uibutton alloc] initwithframe:cgrectmake(10, 90, 20, 20)]; [btn3 settitle:@"button 3" forstate:uicontrolstatenormal]; btn3.tag = 3; [btn3 addtarget:self action:@selector(buttonclick:) forcontrolevents:uicontroleventtouchupinside]; [self.view addsubview:btn3]; 

write button click method following , button tag in it..

-(ibaction)buttonclick:(id)sender {     uibutton *btn = (uibutton *) sender;     nslog(@"%d",btn.tag); } 

Comments

Popular posts from this blog

matlab - Deleting rows with specific rules -

jquery - How would i go about shortening this code? And to cancel the previous click on click of new section? -