0

视图1.h

#import “view2.h"

@interface ViewController : UIViewController 
{
int count;       
}
@property (nonatomic, assign) int count;

在 view1.m

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{

if([segue.identifier isEqualToString:@"rating"])
{
    NSLog(@"identifier: help %@  \n",segue.identifier);

    view2 *vc = [segue destinationViewController];                                                 
    vc.imageNumber = &(count);

}
}

在 view2.h

#import “view1.h"

@interface view2 : UIViewController


@property (nonatomic) int *imageNumber;

在 view2.m

- (void)viewDidLoad
{
[super viewDidLoad];

NSLog(@"imagenumber %@\n",imageNumber);    

}

这里有什么问题?理想情况下,我希望能够双向发送信息......但我只是想弄清楚单向传输有什么问题。谢谢。

4

1 回答 1

2
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{

if([segue.identifier isEqualToString:@"rating"])
{
    NSLog(@"identifier: help %@  \n",segue.identifier);

    view2 *vc = [segue destinationViewController];  
    //viewDidLoad has already happened.                                               
    vc.imageNumber = &(count);

}
}

你检查你设置的 int viewDidLoadviewDidLoad你在的时候已经发生了prepareForSegue。尝试改用viewWillAppearorviewDidAppear看看会发生什么。我认为这一切都应该奏效。

EDIT Sorry i didn't look at your NSLog closely enough. %@ in a format string, like what you use in NSLog is a stand in for an NSString, assumes the argument is an NSObject and tries to call [imageNumber description] which is obviously nonsensical in this case. In fact you need to use the standard integer marker in the format string and then dereference the pointer. The correct NSLog statement would look like this:

NSLog(@"imagenumber %d",*imageNumber);
于 2012-04-17T19:05:27.993 回答