这是一个非常简单的问题。我在一个视图上有一个标签,在前一个视图上有一个UITableView。当用户选择行时我触发了一个segue,我希望用该行中的文本更新标签。这是一个例子,代码很明显。
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *countrySelection;
switch (indexPath.section) {
case kFirstSection:
countrySelection = [[NSString alloc]
initWithFormat:@"The country you have chosen is %@",
[self.MyCountries objectAtIndex: indexPath.row]];
[self performSegueWithIdentifier:@"doneResults" sender:self];
self.countryResult.text = countrySelection;
break;
标签未更新,我只是不知道应该做什么。
提前致谢!
答案 0 :(得分:1)
这些东西确实需要在拥有它们的View Controller上设置。使用公共属性将所选国家/地区的值传递给该视图控制器,如下所述:
首先,创建一个名为:
的属性@property(non atomic,strong) NSString *countryChosen;
在目标视图控制器中,并确保@synthesize
它
没有理由为IndexPath创建另一个属性。只需使用
// Pass along the indexPath to the segue prepareForSegue method, since sender can be any object
[self performSegueWithIdentifier:@"doneResults" sender:indexPath];
然后在prepareForSegueMethod
:
MyDestinationViewController *mdvc = segue.destinationViewController;
NSIndexPath *indexPath = (NSIndexPath *)sender;
mdvc.countryChosen = [self.MyCountries objectAtIndex: indexPath.row]];
在目标VC的viewDidLoad
事件中,只需使用:
self.countryResult.text = countryChosen;
*编辑*
要处理具有多个部分的数据源,只需使用cellForRowAtIndexPath
中的相同逻辑。
N SDictionary *selRow = [[self.countriesIndexArray valueForKey:[[[self.countriesIndexArray allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] objectAtIndex:indexPath.section]] objectAtIndex:sindexPath.row];
更改它以满足您的需要,但基本上您实现的是与显示单元格相同的逻辑,除非您指定了所需的indexPath(包括section和row)。
然后在目标VC上设置该属性:
self.countryResult.text = [selRow valueForKey@"Country"];
答案 1 :(得分:0)
在当前视图控制器中,为用户选择的单元格的indexPath创建一个新属性,如下所示:
@property(strong,nonatomic) NSIndexPath *path;
@synthesize它然后当用户选择一行时,使用
进行设置self.path = indexPath;
执行segue时,它将始终调用
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
因此,当prepareForSegue:被调用时,你现在可以做的是:
/* if this is not the only segue you are performing you want to check on the identifier first to make sure this is the correct segue */
NSString *countrySelection = [[NSString alloc]
initWithFormat:@"The country you have chosen is %@",
[self.MyCountries objectAtIndex: self.path.row]];
segue.destinationViewController.countryResult.text = countrySelection;
/* after creating the text, set the indexPath to nil again because you don't have to keep it around anymore */
self.path = nil;
要使其工作,选择单元格后要显示的视图控制器必须具有UILabel的属性,您尝试在该属性上设置文本。