对应的返回操作函数:[self dismissModalViewControllerAnimated:YES];这里的self代表的是UIViewController自己。
其可设置四种动画模式:
- case 0:
- [ctrl setModalTransitionStyle:UIModalTransitionStyleCoverVertical]; //底部向上 默认的动画方式
- break;
- case 1:
- [ctrl setModalTransitionStyle:UIModalTransitionStyleCrossDissolve]; //渐变
- break;
- case 2:
- [ctrl setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal]; //翻转
- break;
- case 3:
- [ctrl setModalTransitionStyle:UIModalTransitionStylePartialCurl]; //翻半页
- break;
- default:
- [ctrl setModalTransitionStyle:UIModalTransitionStyleCoverVertical]; //底部向上 默认的动画方式
- break;
对应的调用函数:[self.navigationController pushViewController:ctrl animated:YES];
返回:[self.navigationController popViewControllerAnimated:YES];,
也可直接指定返回到那一个ViewController:
- (NSArray *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated
还可以直接返回到RootViewController:- (NSArray *)popToRootViewControllerAnimated:(BOOL)animated
UITabBarContrller对象有一个属性selectedIndex,设置初始时默认展示的ViewController。
而当点击tabBarItem时触发切换操作 都会回调UITabBarControllerDelegate中的一个方法
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *) viewController
方式一(跳转+传值):通过定义一个实体类传值 (从ViewController1 跳转至 ViewController2)
1、定义实体类NotificationEntity
.h声明文件
#import <Foundation/Foundation.h>
@interface NotificationEntity : NSObject
{
}
@property (nonatomic,retain) NSString *strTitle; //参数一
@property (nonatomic,retain) NSString *strContent; // 参数二
@property (nonatomic,retain) NSString *strUrl; //参数三
@end
.m实现文件
#import "NotificationEntity.h"
@implementation NotificationEntity
@synthesize strTitle=_strTitle;
@synthesize strContent=_strContent;
@synthesize strUrl=_strUrl;
@end
2、在ViewController2中
在.h文件中声明实体类NotificationEntity为ViewController2的类成员变量:
@property (retain,nonatomic) NotificationEntity *mNotifEntity;
在.m文件中通过@synthesize为成员变量mNotifEntity合成存取方法:
@synthesize mNotifEntity=_mNotifEntity;
3、在ViewController1中实现跳转并传递参数
// 组装实体类的实例变量
NotificationEntity *mNotificationEntity = [[NotificationEntity alloc] init];
[mNotificationEntity setStrTitle:strTitle];
[mNotificationEntity setStrContent:strContent];
[mNotificationEntity setStrUrl:strUrl];
// 实例化ViewController2
ViewController2 *viewController2 = [[ViewController2 alloc] init];
// 注入参数
[viewController2 setMNotifEntity:mNotificationEntity];
// 跳转
[self.window.rootViewController presentModalViewController:viewController2 animated:YES];
4、在ViewController2中接收参数:
- (void)viewDidLoad
{
[super viewDidLoad];
[self.lblTitle setText:[self.mNotifEntity strTitle]];
[self.lblContent setText:[self.mNotifEntity strContent]];
}
5、在ViewController2中加入返回ViewController1的事件:
- (IBAction)backOff:(id)sender
{
[self dismissModalViewControllerAnimated:YES];
}
切换方式二:xib跳转
在ViewController1中:
1、跳转至ViewController2
ViewController2 *viewController2 = [[ViewController2 alloc]init];
[self presentModalViewController:secondViewanimated:YES];
2、从ViewController2返回至ViewController1:
- (IBAction)backOff:(id)sender
{
[self dismissModalViewControllerAnimated:YES];
}