iOS storyboard中四种好用的界面切换

来源:互联网 发布:js教程 编辑:程序博客网 时间:2024/04/29 04:50

方法一:
UIStoryborad中有个 初始化并返回一个指定制图控制器的方法:instantiateViewControllerWithIdentifier,使用identifier来指定,这里写图片描述,在storyboard 的Identity这里,例如设置为sid2,然后再将根视图控制器强行换掉,换成从当前storyboard中指定的视图控制器:
self.view.window.rootViewController=[self.storyboard instantiateViewControllerWithIdentifier:@”sid2”];

方法二:
UIViewController中有个
- (void)showViewController:(UIViewController *)vc sender:(id)sender 的方法,在你需要切换的地方使用[self showViewController:[self.storyboard instantiateViewControllerWithIdentifier:@”sid2”] sender:nil];是不是感觉跟第一种方法有点相似,但却不同哦。

方法三
UIViewController中 还有个初始化一个segue的方法,segue的作用不用多讲了
- (void)performSegueWithIdentifier:(NSString *)identifier sender:(id)sender
这个identifier依然跟前面的方法一样设置,然后在你需要切换的时候加上:[self performSegueWithIdentifier:@”sid2” sender:nil];

方法四:
稍微复杂一点,新建一个类继承UIStoryboardSegue,例如叫MySeue,UIStoryboardSegue是用来实现两个控制器转变的一个Object,其中有两个重要的属性,sourceViewController和destinationViewController,即源视图控制器和目的视图控制器。
UIStoryboardSegue中 有个 - (void)perform的方法,专门处理segue的。
我们在这个新建的类中对perform方法重写,很好玩喔,你甚至可以控制界面切换的方式如动画效果,
-(void)perform{
UIViewController *src=self.sourceViewController;
UIViewController *dst=self.destinationViewController;
dst.modalPresentationStyle=UIModalPresentationFullScreen;//呈现风格
dst.modalTransitionStyle=UIModalTransitionStylePartialCurl;//转变时的风格
[src showViewController:dst sender:nil];
//src.view.window.rootViewController=dst;
}
然后在待切换的控制器中import一下刚刚定义的类,并加入
-(BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender{return YES;}
因为你得让系统允许触发这个segue,接着实例化你的segue:
MySegue *seg=[[MySegue alloc] initWithIdentifier:@”myr” source:self destination:[self.storyboard instantiateViewControllerWithIdentifier:@”sid5”]];
最后调用方法[seg perform];
大功告成,虽然第四种方法稍微复杂,但是我们可以用这个方法进行更精细的控制。

0 0
原创粉丝点击