方式一:通過定義一個實體類傳值 (從ViewController1 跳轉至 ViewController2)
1、定義實體類NotificationEntity
.h聲明文件
#import
@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];
}