There are three view, View1, View2, VIEW3, respectively, three view switching through the Uisegmentedcontrol.
iOS code
- @interface Uiviewdemoviewcontroller:uiviewcontroller {
- Iboutlet UIView *view1;
- Iboutlet UIView *view2;
- Iboutlet UIView *view3;
- }
- -(Ibaction) Switchviews: (ID) sender;
- @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
- -(void) Viewdidload {
- [Super Viewdidload];
- [Self.view Addsubview:view1];
- [Self.view Addsubview:view2];
- [Self.view ADDSUBVIEW:VIEW3];
- }
- -(Ibaction) Switchviews: (ID) sender{
- Uisegmentedcontrol *segmentedcontrol = sender;
- [[Nsnotificationcenter Defaultcenter] postnotificationname:@"Switchviews" Object:[nsnumber numberWithInteger:[ Segmentedcontrol Selectedsegmentindex]];
- }
Next, create a processing class for the Uisegmentedcontrol control.
iOS code
- @interface Segmentedcontrol:uisegmentedcontrol {
- Iboutlet UIView *view1;
- Iboutlet UIView *view2;
- Iboutlet UIView *view3;
- Iboutlet uiviewdemoviewcontroller* Viewcontroller;
- }
- @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
- -(void) awakefromnib{
- [[Nsnotificationcenter Defaultcenter] Addobserver:self
- Selector: @selector (switchviews:)
- name:@"Switchviews"
- Object:nil];
- }
- -(void) Switchviews: (nsnotification*) notification{
- NSNumber *viewnumber = [Notification Object];
- Nsinteger i = [Viewnumber integervalue];
- [Self setselectedsegmentindex:i];
- UIView *chosenview = nil;
- switch (i) {
- Case 0:
- Chosenview = View1;
- Break
- Case 1:
- Chosenview = View2;
- Break
- Case 2:
- Chosenview = VIEW3;
- Break
- Default
- Break
- }
- if (Chosenview) {
- [[Viewcontroller view] bringsubviewtofront:chosenview];
- }
- }
- -(void) dealloc{
- [Super Dealloc];
- [[Nsnotificationcenter Defaultcenter] removeobserver:self];
- }
The code can be downloaded to the animated effect of the view switch.