如何传递preareForSegue:一个对象我在mapview中有许多注释(与rightCalloutAccessory(按钮)。按钮将从此执行一个segue。mapview转到tableview..我想通过tableview一个不同的对象(保存数据),这取决于单击了哪个标注按钮。例如:(完全是虚构的)注释1(奥斯汀)->传递数据obj 1(与奥斯汀相关)注释2(达拉斯)->传递数据obj 2(与达拉斯相关)注释3(休斯顿)->传递数据obj 3等等.(你知道)我能够检测到哪个呼号按钮被点击了。我在用prepareForSegue*将数据obj传递给目标ViewController..由于我不能让这个调用为我所需的数据obj增加一个参数,那么有哪些优雅的方法可以达到相同的效果(动态数据obj)?任何提示都将不胜感激。
3 回答
泛舟湖上清波郎朗
TA贡献1818条经验 获得超3个赞
prepareForSegue:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{ // Make sure your segue name in storyboard is the same as this line if ([[segue identifier] isEqualToString:@"YOUR_SEGUE_NAME_HERE"]) { // Get reference to the destination view controller YourViewController *vc = [segue destinationViewController]; // Pass any objects to the view controller here, like... [vc setMyObjectHere:object]; }}
performSegueWithIdentifier:sender:
IBAction
performSegueWithIdentifier:
// When any of my buttons are pressed, push the next view- (IBAction)buttonPressed:(id)sender{ [self performSegueWithIdentifier:@"MySegue" sender:sender];}// This will get called too before the view appears- (void)prepareForSegue: (UIStoryboardSegue *)segue sender:(id)sender{ if ([[segue identifier] isEqualToString:@"MySegue"]) { // Get destination view SecondView *vc = [segue destinationViewController]; // Get button tag number (or do whatever you need to do here, based on your object NSInteger tagIndex = [(UIButton *)sender tag]; // Pass the information to your destination view [vc setSelectedButton:tagIndex]; }}
忽然笑
TA贡献1806条经验 获得超5个赞
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{ if ([segue.destinationViewController respondsToSelector:@selector(setMyData:)]) { [segue.destinationViewController performSelector:@selector(setMyData:) withObject:myData]; } }
@property (nonatomic, strong) MyData *myData;
摇曳的蔷薇
TA贡献1793条经验 获得超6个赞
override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if let yourVC = segue.destination as? YourViewController { yourVC.yourData = self.someData }}
- 3 回答
- 0 关注
- 628 浏览
添加回答
举报
0/150
提交
取消