ios -- ViewController跳轉+傳值(方式一)

感謝分享:

轉自:http://www.2cto.com/kf/201402/280001.html


方式一:通過定義一個實體類傳值 (從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];

}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章