没有通过prepareForSegue查看控制器的数据

时间:2015-08-11 09:52:34

标签: ios objective-c

我有一个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

1 个答案:

答案 0 :(得分:2)

viewDidLoad中,更改此行:

NSLog([NSString stringWithFormat:@"name:%@ id:%ld",profileName,(long)profileid]);

到此:

NSLog([NSString stringWithFormat:@"name:%@ id:%ld",self.profileName,(long)self.profileid]);

如果可行,请删除profileNameprofileid ivars并保留属性。如果编译器抱怨,请使用@synthesize作为两个属性。