Reprint please indicate the source
http://blog.csdn.net/pony_maggie/article/details/32163347
Author: Pony
This is a demo demo, the program is actually the last lesson of the psychologist that demo, but on the basis of this demo, make it a universal version, you can automatically identify the device currently running is the iphone or ipad, and then have a different display effect. So, or open the original psychologist project, before the beginning, I will use the lesson five automatic layout knowledge, so that the view can be displayed in the horizontal screen mode. This part is not detailed, refer to lesson five.
Here's how you can make a generic app that fits both the iphone and ipad. We want this to work on the iphone (for a detailed reference to the post of Lesson five):
On the ipad, a split-screen view is used, showing the effect as shown:
OK, here we go. First, change the project configuration, such as:
You can run it right away, select the ipad emulator, then run, and the effect is as follows:
The contrast between the screen and the view is so contrasting that it's like putting the display on the iphone directly onto the ipad and forcing it to stretch, which is ugly. This is because we are running on the ipad or the storyboard on the original iphone. The ipad needs to have its own storyboard. Create a new storyboard method as follows:
The first step is to rename the original storyboard to Iphone.storyboard (name casually, just to differentiate).
The second step is to add a new storyboard, named Ipad.storyboard. Steps such as:
In the third step, specify main interface as the newly created storyboard in the project setup as follows
Ipad.storyboard is empty, first add a split-screen view controller to him. Such as:
Delete the table View controller, this does not use, and then the left side of the two, narrow is the left column (master), wide is the right column (detail), remember the last part of the two concepts. According to the article at the beginning of the view, the right column should be to display the "smiley face", so to put this part of the function reuse, specific methods do not say, in the sixth lesson of the blog, I detailed the steps of reuse, simply speaking is to control the corresponding class set to Happinessviewcontroller, Add a view, the corresponding class is set to Faceview, and then connect the outlet.
The left column is relatively simple to handle, we copy the navigation bar from the Iphone.storyboard, and then paste it into the ipad.storyboard, and then drag some segue to build the push relationship, such as:
Next, we establish the left column and the right column of the connection, whether we remember in the Iphone.storyboard, for Dr.freud, we are first to create a new segue, and then identified, then call Performseguewithidentifier to achieve the switch view effect, then for the ipad, because there is a split-screen view, you can display two views on one screen, so we just send a message to set the happy number of smiling faces, as follows:
This function can determine whether the device currently running is an ipad or an iphone, because only the ipad has split view-(Happinessviewcontroller *) splitviewhappinesscontroller{ id HVC = [self.splitViewController.viewControllers lastobject];//The last one is the right column if (![ HVC Iskindofclass:[happinessviewcontroller class]] { HVC = nil; } return HVC;} -(void) setandshowdiagnosis: (int) diagnosis{ self.diagnosis = diagnosis; if ([self splitviewhappinesscontroller]) { [self splitviewhappinesscontroller].happiness = diagnosis;//directly set } else { [self performseguewithidentifier:@ "showdiagnosis" sender:self]; } }
Notice the Splitviewhappinesscontroller function in the code , which we use to determine whether the device currently running is an ipad or an iphone .(This demo is, of course, for ipad apps that don't have splitview, there are other ways), because only the ipad has a split-screen view, Then the current viewcontroller of the Splitviewconctroller viewcontrollers (a bit around) the property has a split-screen corresponding to the two elements. Here's another question, exactly when it was triggered. Self.Splitviewcontroller.Viewcontrollers have left and right column two elements? Should be when theOnce Splitviewctroller is dragged into the storyboard, all the view controllers in this collection are available.
Well, to here about DR.FREUD related has been set up, you can run to see the effect, space is limited, here is not. Dr.pill related because there are three different segue, the situation is slightly more complicated, but also not difficult, space is limited, here do not deal with, interested can do their own.
Just now in the horizontal screen to debug the program, I have not seen the effect of the vertical screen, it is this:
There is no left column, the right column occupies the entire screen, of course, not what we want, as mentioned in the first section, we expect to have a toolbar in the vertical state, and then the toolbar has a button to pop up the left column of the popover style.
First, we need to assign a proxy (delegate) to the split-screen view controller, and which controller to use as the proxy? Because you want to control the left column, so to consider a few controllers on the left, there are three, it would be natural to choose the navigation root view controller, because the two doctor's controller does not appear every time, and the root view controller is loaded each time, so select their superiors. The code is as follows:
How to implement the Protocol//Whether a state hides the left column-(BOOL) Splitviewcontroller: (Uisplitviewcontroller *) svc Shouldhideviewcontroller: ( Uiviewcontroller *) VC inorientation: (uiinterfaceorientation) orientation{ return NO;
Run to see the effect:
Well, the effect is much better, but we also want to optimize again, because the right side of the column feel squeezed, try popover effect.
We need another two ways to implement the Protocol:
-(void) Splitviewcontroller: (Uisplitviewcontroller *) Svcwillhideviewcontroller: (uiviewcontroller*) Aviewcontroller Withbarbuttonitem: (uibarbuttonitem*) Barbuttonitem Forpopovercontroller: (UIPopoverController *) pc; Called when the view was shown again in the Split view, invalidating the Buttonand popover controller.-(void) Splitviewco Ntroller: (Uisplitviewcontroller *) Svcwillshowviewcontroller: (Uiviewcontroller *) Aviewcontrollerinvalidatingbarbuttonitem: (uibarbuttonitem*) Barbuttonitem;
These two methods are called when the left column is to be hidden (such as from a horizontal screen to a vertical screen) and when the left column is about to be displayed (for example, the vertical screen goes to a horizontal screen). We will display the buttons on the toolbar in the implementation of the first function, and the second function hides the buttons on the toolbar.
Here's a question, who will show and hide the buttons on the toolbar, the answer is the right column view controller, because in the vertical state, only it is displayed, so we should place a toolbar in this view, and then the right column view controller to manipulate the button. Further thinking, how to make the above behavior more general, by adding a protocol, this Protocol can control the display or hide the button, and then the right column view controller to implement this protocol.
With the above theoretical basis, you can begin to act, first add an agreement, as follows;
#import <UIKit/UIKit.h>//Note here, change to Uikit@protocol splitviewbarbuttonitempresenter <NSObject> @property ( Nonatomic, strong) Uibarbuttonitem *splitviewbarbuttonitem; @end
Back to Passviewcontroller, add a method to get the object in the right column, so that you can manipulate the button as follows:
Get right Column object-(ID <SplitViewBarButtonItemPresenter>) splitviewbarbuttonitempresenter{ id DETAILVC = [ Self.splitViewController.viewControllers Lastobject]; if (![ DETAILVC Conformstoprotocol: @protocol (splitviewbarbuttonitempresenter)]) { DETAILVC = nil; } return DETAILVC;}
After the agreement is written, you can take a look at the implementation of the two methods mentioned above, as follows:
How to implement the Protocol//Whether a state hides the left column-(BOOL) Splitviewcontroller: (Uisplitviewcontroller *) svc Shouldhideviewcontroller: ( Uiviewcontroller *) VC inorientation: (uiinterfaceorientation) orientation{//Note Understanding this line of code, it has two meanings,// One is if you do not get to the right column controller or the controller does not implement the Protocol, directly return no, let the left column always show//second, if one is not established, hide the left column on the vertical screen return [self splitviewbarbuttonitempresenter]? Uiinterfaceorientationisportrait (orientation): NO;} The left column will be called when it is hidden (e.g. from a horizontal screen to a vertical screen)-(void) Splitviewcontroller: (Uisplitviewcontroller *) svc Willhideviewcontroller: ( Uiviewcontroller *) Aviewcontroller Withbarbuttonitem: (Uibarbuttonitem *) Barbuttonitem ForPopoverController: ( Uipopovercontroller *) pc{//Do two things//one, the title of the toolbar button is set to Self.title (Here is "Doctor")//two, put the button on the toolbar, this is done by the right column, because this time only the right side The bar is displayed. Barbuttonitem.title = Self.title; [Self Splitviewbarbuttonitempresenter].splitviewbarbuttonitem = Barbuttonitem; }//to be called when the left column is to be displayed (for example, by a vertical screen)-(void) Splitviewcontroller: (Uisplitviewcontroller *) svc Willshowviewcontroller: ( Uiviewcontroller *) Aviewcontroller InvalidatiNgbarbuttonitem: (Uibarbuttonitem *) barbuttonitem{//Remove the buttons on the toolbar because the left column is displayed, so there is no need for [self splitviewbarbuttonitempresenter ].splitviewbarbuttonitem = nil;}
Finally, to manipulate the right-hand column view controller, first in the storyboard, give him a toolbar, and then delete the default button (we have to control, so do not bring it) and to the toolbar to connect a outlet for ease of operation. Then inherit the protocol and implement the Code as follows:
Setter Method-(void) Setsplitviewbarbuttonitem: (Uibarbuttonitem *) splitviewbarbuttonitem{ if (_ Splitviewbarbuttonitem! = Splitviewbarbuttonitem) { Nsmutablearray *toolbaritems = [Self.toolBar.items Mutablecopy]; if (_splitviewbarbuttonitem) { //delete the original [ToolbarItems Removeobject:_splitviewbarbuttonitem]; } if (Splitviewbarbuttonitem) { [ToolbarItems insertobject:splitviewbarbuttonitem atindex:0];//On the far left } self.toolBar.items = ToolbarItems; _splitviewbarbuttonitem = Splitviewbarbuttonitem; }}
Well, run it, and you'll see the results from the beginning of the article.
Code:
http://download.csdn.net/detail/pony_maggie/7502567