在 ActionSheet 中如何调用 segue 并且为 destinationViewController 赋值?

2012-12-06 16:36:13 +08:00
 Brutal
用户点击某一个元素后调用 actionSheet:clickedButtonAtIndex: 这个方法。
然后在这个方法中我需要跳转到下一个 view ,同时那个 view 需要一些赋值,比如

TestViewController *vc = [[TestViewController alloc] init];
vc.needValue = buttonIndex;

然后再实现 segue 跳转到 vc。

我试过用 performSegueWithIdentifier:sender: 它调用的是 prepareForSegue:sender: ,所以无法在这个方法中完成 actionSheet:clickedButtonAtIndex: 中的赋值?

求解。。
5174 次点击
所在节点    iDev
4 条回复
eyeplum
2012-12-07 10:40:44 +08:00
TestViewController *vc = [[TestViewController alloc] init];
vc.needValue = buttonIndex;

这里赋值不是这么赋的。你要找到 Segue 的实例 vc,而不是自己 init 一个。

// Get reference to the destination view controller
TestViewController *vc = [segue destinationViewController];

// Pass any objects to the view controller here, like...
vc.needValue = buttonIndex;

看这里:http://stackoverflow.com/questions/7864371/ios5-how-to-pass-prepareforsegue-an-object
eyeplum
2012-12-07 10:58:10 +08:00
补充:如果你想要 actionSheet:clickedButtonAtIndex: 方法里的 buttonIndex 参数,我个人的做法是新建一个 property,比如 self.buttonIndexTapped 然后:
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
// ... 其他代码

self.buttonIndexTapped = buttonIndex;

// ... 其他代码
}
Brutal
2012-12-07 11:38:52 +08:00
@eyeplum 我最后也是这么做的,thx!
zhigang1992
2012-12-07 17:29:12 +08:00
[self performSegueWithIdentifier:@"aIdentifier" sender:theActionSheet];

然后,

prepareForSegue:sender里面就可以取道actionSheet的值了。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/54242

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX