![](/img/trans.png)
[英]Can't pass data to another View Controller with the prepareForSegue method if that View Controller is not the one immediately being segued to
[英]Data not being past to view controller with prepareForSegue
我有一个segue,它在被触发时应该发送一些数据,所以我设置了以下方法:
- (void)viewDidLoad {
[super viewDidLoad];
UITapGestureRecognizer *proFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleProTap:)];
[self.sharePost addGestureRecognizer:proFingerTap];
...
}
- (void)handleProTap:(UITapGestureRecognizer *)recognizer {
[self performSegueWithIdentifier:@"profileToPost" sender:self];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"profileToPost"]) {
ProPostViewController *destViewController = segue.destinationViewController;
NSLog([NSString stringWithFormat:@"name:%@ id:%ld",_profileName,(long)_profileid]);
destViewController.profileid = _profileid;
destViewController.profileName = _profileName;
}
}
在deist视图控制器中,我在头文件中包含以下内容:
@interface ProPostViewController : UIViewController <NSURLConnectionDelegate,NSURLConnectionDataDelegate>{
NSInteger profileid;
NSString *profileName;
}
@property (nonatomic) NSInteger profileid;
@property (nonatomic) NSString *profileName;
接着:
@implementation ProPostViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSLog([NSString stringWithFormat:@"name:%@ id:%ld",profileName,(long)profileid]);
if (profileid != 0){
[_postTitle setText:[NSString stringWithFormat:@"Posting to %@",profileName]];
}
// Do any additional setup after loading the view.
}
但是代码没有从应该在此期间发生的两个NSLog中传递数据,因此我得到:
2015-08-11 10:48:53.653 APP[1705:119528] name:Alex id:3
2015-08-11 10:48:53.685 APP[1705:119528] name:(null) id:0
在您的viewDidLoad
,更改以下行:
NSLog([NSString stringWithFormat:@"name:%@ id:%ld",profileName,(long)profileid]);
对此:
NSLog([NSString stringWithFormat:@"name:%@ id:%ld",self.profileName,(long)self.profileid]);
如果可行,请删除profileName
和profileid
ivars,然后将属性保留在原位。 如果编译器抱怨,请对两个属性使用@synthesize
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.