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

Ios 将参数从一个xib传递到另一个xib,ios,Ios,我的secondviewcontroller中有标签。我想将按钮索引从firstviewcontroller传递到secondviewcontroller标签。当我按下按钮时,它会转到第二个viewcontroller,但标签为零 //FirstViewController.m NSInteger index = [carousel indexOfItemViewOrSubview:sender]; int ind=index; SecondViewController *sVC = [[Sec

我的secondviewcontroller中有标签。我想将按钮索引从firstviewcontroller传递到secondviewcontroller标签。当我按下按钮时,它会转到第二个viewcontroller,但标签为零

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

}
//SecondViewController.h

@property (strong, nonatomic) IBOutlet UILabel *myLabel;
//SecondViewController.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];
@synthesize myLabel;
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    NSLog(@"%@",myLabel.text);

}

在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];

谢谢,它很管用!唯一的问题是,它应该在@property(nonatomic)NSInteger*索引之后,否则它会返回一个error.Cool。可能想把它标记为已回答,以便将来能帮助其他人。是的,我的错。NSNumber需要指针,但NSInteger不需要。我刚修改过。你会在NSInteger上使用什么样的参考计数器?弱?表示
sVC.myLabel.text=…
的行没有意义
myLabel
无疑将是
nil
(这是OP的原始问题)。另外,您刚刚建议他去掉此行使用的
ind
变量。我将删除这整行,因为他将在目标的
viewDidLoad
中执行此操作。问题是目标的视图及其所有
IBOutlet
引用尚未配置。您必须推迟使用
IBOutlet
引用,直到目标视图控制器中的
viewDidLoad
。因此,这就是为什么TooManyEduardos建议创建一个新属性来保存
viewDidLoad
可以引用的值。与您最初的问题无关,
presentModalViewController
被弃用,取而代之的是
presentViewController
。如果需要支持5.0.0之前的iOS版本,请仅使用presentModalViewController。你对下面的问题有什么想法吗?我相信一些iCarousel用户会回答你的问题。我不知道那个框架,所以我不能不花时间去挖掘那个库就提供建议。顺便说一句,不要忘记让
viewdide出现
调用
super
方法…感谢Rob的帮助。