在项目中没有创建 UINavgationController,无法使用默认的 push 方法 进行页面的跳转时。
使用另一种页面跳转方法 :
- (void)presentViewController:(UIViewController *)viewControllerToPresent animated: (BOOL)flag completion:(void (^ __nullable)(void))completion NS_AVAILABLE_IOS(5_0);
常用的方法:
// 结合控件的点击事件,例:UIButton
UIButton * button = [UIButton alloc] init];
[button addTarget:self action:@selector(buttonClick) forControlEvents:(UIControlEventTouchUpInside)];
// 默认跳转方式,从底部推出
- (void)buttonClick {
UIViewController * viewController = [UIViewController alloc] init];
[self presentViewController: viewController animated:YES completion:nil];
}
根据需求设置跳转时的样式:
- (void)buttonClick {
CATransition * animation = [CATransition animation];
animation.duration = 0.5; // 时间
/** type:动画类型
* pageCurl 向上翻一页
* pageUnCurl 向下翻一页
* rippleEffect 水滴
* suckEffect 收缩
* cube 方块
* oglFlip 上下翻转
*/
animation.type = @"pageCurl";
/** type:页面转换类型
* kCATransitionFade 淡出
* kCATransitionMoveIn 覆盖
* kCATransitionReveal 底部显示
* kCATransitionPush 推出
*/
animation.type = kCATransitionPush;
//PS:type 更多效果请 搜索: CATransition
/** subtype:出现的方向
* kCATransitionFromRight 右
* kCATransitionFromLeft 左
* kCATransitionFromTop 上
* kCATransitionFromBottom 下
*/
animation.subtype = kCATransitionFromRight;
[self.view.window.layer addAnimation:animation forKey:nil]; // 添加动作
[self presentViewController: viewController animated:YES completion:nil]; // 跳转
}
// PS:设置 type 属性时,两种写法
在 UIViewController 中才可以调用 presentViewController 方法。
但如果是监听 [自定义View] 中的一个 [UIButton事件] 进行跳转:
// 自定义 View.h 文件中
@property (nonatomic, strong) UIViewController * viewController;
---------------------
// 自定义 View.m 文件中
// 自定义View视图中的 UIButton
self.button = [UIButton alloc] init];
[self.button addTarget:self action:@selector(selfButtonClick:) forControlEvents:(UIControlEventTouchUpInside)];
// self.button 点击方法
- (void)selfButtonClick:(UIViewController *)otherVC {
otherVC = self.viewController;
// do soming
[self presentViewController:otherVC animated:YES completion:nil]; // 跳转
}
-------------------
// 绑定 自定义View 的 Controller 中
self.自定义View.viewController = self;