objective c - Switching between 3 pages with segmentend button requires 2 taps? -


here setup :

home page, profile page, friends page.

in each page, there exact same segmentend button : [home | profile | friends]

when tap on segment, segue called , page changed.

so, when i'm in profile, profile selected, , can tap on either home or friends ; when do, new page loaded, new segment selected , can again if want to.

in storyboard, have 3 pages " ton " of custom segue (i used custom animation purposes).

in code, have :

(void)movetopage {     if ([self.pageswitch selectedsegmentindex] == 1)     {          [self performseguewithidentifier:@"fromhometoprofile" sender:self];;     }     if ([self.pageswitch selectedsegmentindex] == 2)     {          [self performseguewithidentifier:@"fromhometofriends" sender:self];;     }  }   - (ibaction)pageswitchbutton:(id)sender {     [self.pageswitch addtarget:self                            action:@selector(movetopage)                  forcontrolevents:uicontroleventvaluechanged]; } 

the names self explanatory.

my problem (we're getting it) : when tap on segment, becomes selected, nothing happens first time. if tap new segment (that not 1 current page), works fine. basically, first tap ignored, second 1 works. have tap page, , tap isn't same work.

any idea? i'm kind of stuck unique problem :d

okay found ; didn't understand how addtarget:action:forcontrolevents works.

we need put the

[self.pageswitch addtarget:self                            action:@selector(movetopage)                  forcontrolevents:uicontroleventvaluechanged]; 

in viewdidload , works fine. has put in 3 pages.

here explanation got irl, feel free comment/add stuff if i'm wrong :

the addtarget:action:forcontrolevents: method behaviour page has. " when happens, ". because in action, page learned behaviour when clicked first time, , knew how react afterwards. page know first time, has learn behaviour @ earliest stage of creation, hence in viewdidload.

because there nothing left in action, can remove completly , remove links in storyboard. need outlet , movetopage method work.

i hope helps :)


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -