iOS: storyboard (2)

125 篇文章 1 订阅

关于interface的基本术语

*Viewcontroller:用来处理user和app的交互。

*View:它表示user在app上看到的界面,一个app可以有多个view。当然一个view就会对应一个viewcontroller

* Scene:viewcontroller + view就可以看作是一个scene.

* Segue:A segue表示scene之间的切换。有几种类型的Segue,使用哪种取决于你使用哪个类型的viewcontroller

* Modalview:就是会弹出一个view,你只能在该view上操作,而不能切换到其他view,除非你关闭了modalview.Modal View对应的segue type就是modal segue

* Relationship:A “segue” ofsorts for certain types of view controllers, such as the tab bar controllerRelationshipsarecreated between buttons on a master tab bar that display independent sceneswhen touched.

*Storyboard:包含所有的scene, segue, andrelationship definitions

 

 

往storyboard添加新的view controller后,该scenename总是"View controller scene",如何修改它?选中该scene的"viewcontroller" node,然后在右边panel的identityinspector里找到"Identity > Label",修改它。

 

 

SharingProperties and Methods with#importand@class

Class A要引用到Class B,那么就要在classA里import class B。

 

例如,if MyEditorClass needs to access properties and methods inMyGraphicsClass, MyEditorClass.h would include#import “MyGraphicsClass.h”at its start.

 

问题来了,若Class之间相互引用,例如classA引用class B,class B同时也引用classA,那么两边都import对方时会出错。因此就要在.h里使用@class,在.m里使用#import.而且使用@class,编译会快。

 

所以不管是不是相互引用,我觉得最好都在.h里使用@class,在.m里使用#import,省心!

 

参考文档:

http://blog.prosight.me/index.php/2009/09/347

http://blog.eddie.com.tw/2010/12/08/class-directive-in-objective-c/

 

 

Creatinga Segue

很简单,只需要选中该button,然后按住Ctrl,drag到要切换到的view即可。

 

当你drag and release mouse后,在targetview里会弹出一个菜单让你选择Seguetype。主要有5个segue types (大部分情况下使用Modalsegue type):

*Modal:Transition to another scene for the purposes ofcompleting a task.user在弹出的modalview里操作完后,就应该dismiss themodal view scene然后切换回the originalview.

*Push:Create a chain of scenes where theuser can move forward or back.该segue type是和navigation viewcontrollers一起使用

*Replace (iPad only):Replace thecurrent scene with another. This is used in some specialized iPad viewcontrollers (e.g. split-view controller).

*Popover(iPad only):Displays thescene in a pop-up “window” over top of the current view.

*Custom:Used for programming a customtransition between scenes.

 

Configuringa Segue

一旦创建了Segue,就会在对应的Scene的"viewcontroller"里面添加了一个"Seguefrom <origin> to <destination>" node。


 

选中该node,在右边的Utilitiespanel里可以对Segue进行设置。


 

其中最重要的是"Identifier"属性,通过它你可以手动(即写代码)来trigger a segue,还可以在有多个segues时进行识别。即使你不使用多Segue,也建议为segue设置"Identifier"

 

ControllingModal Segues Manually

上面创建的segue不需要写任何代码就可以实现clicka button就会切换到另一个view。但有时我们还是需要通过代码来trigger一个segue来进行view切换。比如收到一个message,就切换到显示该message的view,这就需要写代码来triggersegue。

 

在一个UIViewController里trigger segue来弹出modal view:

                [selfperformSegueWithIdentifier:@”toMyGame” sender:self];

第一个参数就是segueidentifier.

 

Modal view关闭自己并返回到originalview (这个功能必须通过代码,因为在storyboard里没有反向seguefor modal):

                [selfdismissViewControllerAnimated:YES completion:nil];

注意:这行代码可以写在Modalview controller里,也可以写在originalview controller。

 

 

 

Programminga Modal Scene Switch

上面2 part都是讲解通过Segue来切换view/scene。这部分讲解如何不通过Segue,而直接写代码来实现ModalScene切换(即写代码来实现Segue的功能)。

 

step 1 获取storyboard实例:

                UIStoryboard*mainStoryboard=[UIStoryboard storyboardWithName:@”MainStoryboard” bundle:nil];

 

step 2 获取view and view controller 实例:

                EditorViewController*editorVC=[mainStoryboard instantiateViewControllerWithIdentifier:@”myEditor”];

 

step 3 用代码手动设置view的displaystyle为modal:

                editorVC.modalTransitionStyle=UIModalTransitionStyleCoverVertical;

                editorVC.modalPresentationStyle=UIModalPresentationFormSheet;

 

> Transition styles:

UIModalTransitionStyleCoverVertical,

UIModalTransitionStyleFlipHorizontal,

UIModalTransitionStyleCrossDissolve, or

UIModalTransitionStylePartialCurl

>Presentationstyles:

UIModalPresentationFormSheet,

UIModalPresentationPageSheet,

UIModalPresentationFullScreen, or

UIModalPresentationCurrentContext

 

 

step 4 显示view controller对应的view (上面的代码设置了该view以modalstyle显示)

                [selfpresentViewController:editorVC animated:YES completion:nil];

 

 

 

Passing Data Between Original scene and Modal scene

注意:不同类型的scene,传递data的方式不同。Modal scene是使用presentingViewController, presentedViewController,而navigation controller和tab bar controller是使用parentViewController

 

如果你是象上part说的通过写代码来实现view切换,那么可以在获取下一个view的实例时,调用它的propertyor method来传递。例如在上part的代码中,you already have aninstance of the new scene’s view controller in

yourinitial view controller,Youcan set/access properties on the new view controller

(editorVC.myImportantProperty=<value>)beforedisplaying it and after it is dismissed.

 

 

如果你是通过segue来切换view的话,那么你可以通过prepareForSegue:sendermethod来传递data。该方法是写在originalview controller里,当该controller trigger a segue来切换view时,会自动调用该方法。该方法有一个"segue"参数,它是UIStoryboardSegue对象,有2个很重要的属性"sourceViewController"and "destinationViewController",用来表示theview controller starting the segue (the source) and the view controller aboutto be displayed (the destination)

 

例:

-(void)prepareForSegue:(UIStoryboardSegue *)seguesender:(id)sender {

ViewController*startingViewController;

EditorViewController *destinationViewController;

startingViewController=(ViewController*)segue.sourceViewController;//注意:这里segue.sourceViewController可以用self代替

destinationViewController=(EditorViewController *)segue.destinationViewController;

destinationViewController.xxxxx=xxxx;//pass data

}

 

注意:若你的origin viewcontroller里定义了多个segue,那么每个segue被trigger时都会调用"prepareForSegue"方法,怎样区别是哪个segue被trigger?就要用到segue identifier:

 

-(void)prepareForSegue:(UIStoryboardSegue *)seguesender:(id)sender {

if ([segue.identifierisEqualToString:@”myAwesomeSegue”]) {

                //Do something unique for this segue

}

}

 

 

prepareForSegue method只提供了从originalview传data给modal view的通道。当user在modalview操作完,在dismiss modal view时如何把data从modalview传回给original view?prepareForSegue method做不到回传data.

 

回传data,一个不值得推荐的方式就是:create a property on thedestination controller that stores a reference to the source controller.

 

另一个更简单的方法是使用UIViewController class的缺省built-inproperties:presentingViewControllerandpresentedViewController.

 

在modal view里如果要获取originalview,可以在modal view controller里调用

        ((ViewController*)self.presentingViewController).<property>

 

在original view里如果要获取目前已经在显示的modalview,则可以在original view controler里调用

        ((EditorViewController*)self.presentedViewController).<property>

 

那么上面所说的问题,就可以在modalview里通过presentingViewController来获取original view的引用。

 

 

 

 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值