将参数从一个xib传递到另一个xib

时间:2014-12-30 16:59:36

标签: ios

我的secondviewcontroller中有标签。我想将按钮索引从firstviewcontroller传递到secondviewcontroller标签。当我按下按钮时,它会转到第二个视图控制器,但标签是nil

// FirstViewController.m

NSInteger index = [carousel indexOfItemViewOrSubview:sender];
int ind=index;
SecondViewController *sVC = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:[NSBundle mainBundle]];
sVC.myLabel.text=[NSString stringWithFormat:@"%d",ind];
[self presentModalViewController:sVC animated:YES];

// SecondViewController.h

@property (strong, nonatomic) IBOutlet UILabel *myLabel;

// SecondViewController.m

@synthesize myLabel;
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    NSLog(@"%@",myLabel.text);

}

1 个答案:

答案 0 :(得分:2)

在SecondViewController.h中添加另一个属性:

@property (nonatomic) NSInteger index;

然后在FirstViewController.m中将index的值传递给第二个视图的索引:

NSInteger index = [carousel indexOfItemViewOrSubview:sender];
int ind=index;  //now you don't need this

SecondViewController *sVC = [[SecondViewController alloc]     initWithNibName:@"SecondViewController" bundle:[NSBundle mainBundle]];
sVC.myLabel.text=[NSString stringWithFormat:@"%d",ind];

// New line
sVC.index = index;

[self presentModalViewController:sVC animated:YES];