在视图控制器之间传递变量
问题描述:
我正在使用此函数在Xcode 4.3中的视图之间切换。在视图控制器之间传递变量
[self performSegueWithIdentifier:@"NextView" sender:self];
我想在页面之间传递一些参数。我怎样才能做到这一点?
答
performSegueWithIdentifier:sender:
后您的观点。控制器会调用
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
假设你的新视图控制器具有一些propertys to set:
if ([[segue identifier] isEqualToString:@"NextView"]) {
MyViewController *myVC = [segue destinationViewController];
myVC.propertyToSet = // set your properties here
}
答
这个问题有传递数据的视图控制器之间的一个非常好的和正确的解释:
+1
是的,这是很好的解释,但他们正在使用pushViewController视图更改,但我想使用performSegueWithIdentifier。 – bla0009 2012-07-20 10:47:21
答
这里是Pfitz答案,但我做了一点比较容易理解为乡亲使用例如谁是新的Objective-C:
-
在你destinationViewController.h文件中添加属性设置变量
// VOLViewController.m #import "VOLVoteViewController.h
:从您的源文件Controller.m或者
导入你的destinationViewController.h您sourceViewController.m文件 '接收' 值
-
添加
prepareForSegue
方法您sourceViewController.m文件和传递变量- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { VOLVoteViewController *myDestinationViewController = [segue destinationViewController]; // myDestinationViewController.variable = sourceViewController variabe myDestinationViewController.billIdReceivingVote = [self.idOfBillSelected intValue]; }
@property (nonatomic) int billIdReceivingVote;
没有ü尝试使用导航控制器代替。它是非常简单的通过值 – WaaleedKhan 2012-07-20 10:30:01
不,我没有尝试导航控制器。我在故事板中使用常规按钮并继续播放。 – bla0009 2012-07-20 11:12:09