在移动应用APP中多界面开发往往是必不可少的,像我们平时应用的APP,一个界面跳转到另一个界面,一个界面又是如何回到上一个界面,这里我会分别从XIB和StoryBoard两个方面出发会给大家举例说明,最近新研究了一下APP的融合,也就是两个APP融合为一个APP(把其中一个APP的功能作为另一个APP的子功能)
一.工程里面的控制器是附带XIB创建或者不带XIB
1.在模态下跳转:
YourViewController *yourVC = [[YourViewController alloc]init];
[self presentViewController:yourVC animated:YES completion:nil];
返回上一个界面:
[self dismissViewControllerAnimated:YES completion:nil];
2.在导航控制器下跳转:
YourViewController *yourVC =[[YourViewController alloc] init];
[self.navigationController pushViewController: yourVC animated:YES];
返回上一个界面:
[self.navigationController popViewControllerAnimated:YES];
二.利用storyboard实现界面跳转
1.使用segue自动跳转
①图例如下:
两个segue对象链接着同一个场景,用identifer区分这两个segue
1.jpeg
绑定控制器
2.jpeg
②代码如下:
ViewController.m
#import "ViewController.h"
#import "PinkViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
//segue即将发生跳转之前触发
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"PushSegue"]) {
//获取目标场景,目标vc
PinkViewController *pinkVC = segue.destinationViewController;
pinkVC.segueType = @"Push";
return;
}
if ([segue.identifier isEqualToString:@"ModelSegue"]) {
PinkViewController *pinkVC = segue.destinationViewController;
pinkVC.segueType = @"Model";
return;
}
}
@end
PinkViewController.h
#import "ViewController.h"
@interface PinkViewController : ViewController
@property (nonatomic ,strong)NSString *segueType;//设置接收属性
@end
PinkViewController.m
#import "PinkViewController.h"
@interface PinkViewController ()
@property (weak, nonatomic) IBOutlet UILabel *showSegueLabel;
@end
@implementation PinkViewController
- (void)viewWillAppear:(BOOL)animated {
self.showSegueLabel.text = self.segueType;
}
- (void)viewDidLoad {
[super viewDidLoad];
}
- (IBAction)goBackButtonClick:(id)sender {
[self dismissViewControllerAnimated:YES completion:nil];
}
@end
2.使用代码的方式实现跳转
①图例如下:
创建控制器类并绑定控制器,给greenViewController添加storyboardID添加标识
3.jpeg
②代码如下:
-(IBAction)gotoGreenVC:(id)sender {
//1.获取故事版
UIStoryboard *storyboard =self.storyboard;
//2.由故事板负责创建标示为greenVC的这个场景实例
//而不是由我们使用initWithName这个方法来创建
greenViewController *greenVC =[storyboard instantiateViewControllerWithIdentifier:@"greenVC"];
[self.navigationController pushViewController:greenVCanimated:YES];
}
3.在storyboard跳转到xib定义的VC
①图例如下:
4.jpeg
②代码如下:
-(IBAction)gotoBlueVC:(id)sender {
BlueViewController *blueVC =[[BlueViewController alloc]initWithNibName:@"BlueViewController" bundle:nil];
blueVC.navigationItem.title = @"BlueVC";
[self.navigationController pushViewController:blueVCanimated:YES];
}
4.storyboard跳转到storyboard
//通过window 没有动画
-(void)touchesBegan:(NSSet *)toucheswithEvent:(UIEvent *)event
//创建story对象 通过名字 拿到目标的故事板
UIStoryboard*story=[UIStoryboard storyboardWithName:@"Storyboard2" bundle:nil];
//跳转到目标故事板 初始的vc
self.view.window.rootViewController=[story instantiateInitialViewController];
}
网友评论