Go Switch between view on IOS segment page

Source: Internet
Author: User

There are three view, View1, View2, VIEW3, respectively, three view switching through the Uisegmentedcontrol.

iOS code
    1. @interface Uiviewdemoviewcontroller:uiviewcontroller {
    2. Iboutlet UIView *view1;
    3. Iboutlet UIView *view2;
    4. Iboutlet UIView *view3;
    5. }
    6. -(Ibaction) Switchviews: (ID) sender;
    7. @end

In Interface Builder, three view is created, associated to the respective output port, and the Uisegmentedcontrol on each view is associated to the Switchviews: operation.

iOS code
    1. -(void) Viewdidload {
    2. [Super Viewdidload];
    3. [Self.view Addsubview:view1];
    4. [Self.view Addsubview:view2];
    5. [Self.view ADDSUBVIEW:VIEW3];
    6. }
    7. -(Ibaction) Switchviews: (ID) sender{
    8. Uisegmentedcontrol *segmentedcontrol = sender;
    9. [[Nsnotificationcenter Defaultcenter] postnotificationname:@"Switchviews" Object:[nsnumber numberWithInteger:[  Segmentedcontrol Selectedsegmentindex]];
    10. }

Next, create a processing class for the Uisegmentedcontrol control.

iOS code
    1. @interface Segmentedcontrol:uisegmentedcontrol {
    2. Iboutlet UIView *view1;
    3. Iboutlet UIView *view2;
    4. Iboutlet UIView *view3;
    5. Iboutlet uiviewdemoviewcontroller* Viewcontroller;
    6. }
    7. @end

Change the class of the Uisegmentedcontrol control to the processing class you just created: Segmentedcontrol, then associate the Uisegmentedcontrol control to four outputs: The view is associated to the three view you just created, The Viewcontroller is associated to the file ' s owner.

iOS code
  1. -(void) awakefromnib{
  2. [[Nsnotificationcenter Defaultcenter] Addobserver:self
  3. Selector: @selector (switchviews:)
  4. name:@"Switchviews"
  5. Object:nil];
  6. }
  7. -(void) Switchviews: (nsnotification*) notification{
  8. NSNumber *viewnumber = [Notification Object];
  9. Nsinteger i = [Viewnumber integervalue];
  10. [Self setselectedsegmentindex:i];
  11. UIView *chosenview = nil;
  12. switch (i) {
  13. Case 0:
  14. Chosenview = View1;
  15. Break
  16. Case 1:
  17. Chosenview = View2;
  18. Break
  19. Case 2:
  20. Chosenview = VIEW3;
  21. Break
  22. Default
  23. Break
  24. }
  25. if (Chosenview) {
  26. [[Viewcontroller view] bringsubviewtofront:chosenview];
  27. }
  28. }
  29. -(void) dealloc{
  30. [Super Dealloc];
  31. [[Nsnotificationcenter Defaultcenter] removeobserver:self];
  32. }

The code can be downloaded to the animated effect of the view switch.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.