从视图控制器读取传递的数据
Posted
技术标签:
【中文标题】从视图控制器读取传递的数据【英文标题】:reading passed data from view controller 【发布时间】:2015-04-22 09:22:09 【问题描述】:我有 2 个视图控制器, 我想在它们之间传递数据,在第一个视图控制器中我使用此方法切换到其他视图控制器并传递数据:
- (void)loginViewFetchedUserInfo:(FBLoginView *)loginView
user:(id<FBGraphUser>)user
NSString *userName = [user username];
NSString *userId = [user id];
dashBoardViewController *dashBoardViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"dashBoardViewController"];
dashBoardViewController.first_name = userName;
dashBoardViewController.id = userId;
[self presentModalViewController:dashBoardViewController animated:YES];
在 view controller.h 上有这个
@interface ViewController : UIViewController <FBLoginViewDelegate>
@property (weak, nonatomic) IBOutlet FBLoginView *loginButton;
@property (weak, nonatomic) IBOutlet UIButton *showEvent;
@property (weak, nonatomic) IBOutlet UIButton *goQrcode;
@property (retain, nonatomic) NSString *id;
@property (retain, nonatomic) NSString *first_name;
在第二个视图控制器中,我有这个方法来读取变量并切换到另一个视图控制器:
- (IBAction)gotoEvents:(id)sender
events *events;
events.first_name = _first_name;
events.userid = _id;
[self.storyboard instantiateViewControllerWithIdentifier:@"events"];
[self presentModalViewController:events animated:YES];
这在我的头文件中:
@property (nonatomic, retain) NSString *first_name;
@property (nonatomic, retain) NSString *id;
- (IBAction)gotoEvents:(id)sender;
它说我 在“事件”类型的对象上找不到属性“名字”
【问题讨论】:
使用强格式 first_name 属性代替保留...! 您在使用情节提要吗?如果是这样,那么您应该使用prepareForSegue:
【参考方案1】:
试试这个,
events *events = [self.storyboard instantiateViewControllerWithIdentifier:@"events"];
events.first_name = _first_name; //class name Should Start With capital letter and should use camel case for naming.
events
应该命名为Events
,因为它是类名
【讨论】:
以上是关于从视图控制器读取传递的数据的主要内容,如果未能解决你的问题,请参考以下文章