gpt4 book ai didi

ios - 使用 addTarget : action: forControlEvents: 将 indexPath.row 移动到标签

转载 作者:行者123 更新时间:2023-11-29 02:56:26 25 4
gpt4 key购买 nike

我正在尝试将我的 indexPath.row 移动到不同的 View Controller - pageView 标签。这是我尝试过的:

[cell.nav addTarget:self action:@selector(naviguate:) forControlEvents:UIControlEventTouchUpInside];
cell.nav.tag=indexPath.row;

-(void)naviguate:(id)sender {
UIButton *theButton=(UIButton *)sender;
NSLog(names[theButton.tag]);
pageView *secondViewController = [[pageView alloc] initWithNibName:@"secondViewController" bundle:nil];

secondViewController.name.text = names[theButton.tag];
}

为什么这不会更改 pageView 上的标签文本。我应该怎么做?


Logs 字符串工作正常的地方。


为@danh 编辑

我试过这个:

-(void)naviguate:(id)sender {
[UIView animateWithDuration:0.5
delay:0
options: UIViewAnimationOptionCurveEaseOut
animations:^{

[_tableView setFrame:CGRectMake(0, 569, _tableView.frame.size.width, _tableView.frame.size.height)];
}
completion:^(BOOL finished){
[self performSegueWithIdentifier:@"link" sender:self];
}];

}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
UIButton *theButton=(UIButton *)sender;

if([segue.identifier isEqualToString:@"link"])
{
UINavigationController *navController = (UINavigationController *)segue.destinationViewController;
pageView *controller = (pageView *)navController.topViewController;
controller.name.text= names[theButton.tag];
}
}

它被调用的地方

[cell.nav addTarget:self action:@selector(naviguate:) forControlEvents:UIControlEventTouchUpInside];
cell.nav.tag=indexPath.row;

但我收到错误 [pageView topViewController]: unrecognized selector sent to instance ... Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[pageView topViewController]: unrecognized selector sent to instance

你知道为什么吗?


第二次尝试

与:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
UIButton *theButton=(UIButton *)sender;
if([segue.identifier isEqualToString:@"link"])
{
pageView *controller = (pageView *)segue.destinationViewController;
controller.name.text= names[theButton.tag];
}
}

我收到错误 '-[ImagesTableViewController tag]: unrecognized selector sent to instance

WHich 因为 names[theButton.tag] 崩溃了怎么办?

最佳答案

这一行:

pageView *secondViewController = [[pageView alloc] initWithNibName:@"secondViewController" bundle:nil];

创建一个新的 View Controller ,然后这一行:

secondViewController.name.text = names[theButton.tag];

设置其中一个 View 的属性(这是错误的,因为 View 还没有加载,而且乱用另一个 vc 的 View 是不明智的)。但这并不重要,因为这一行:

}

销毁新分配的 vc,它将永远不会再被听到。

修复的方法是从起始 View Controller 使用 segue,并在 prepareForSegue: 中设置目标属性,或者推送或呈现 secondViewController。要么:

// if we're in a navigation controller
[self.navigationController pushViewController:secondViewController animated:true];

// or, if there's no container
[self presentViewController:secondViewController animated:true completion:nil];

请记住,无论您决定如何呈现 secondViewController,都不要尝试直接设置其中一个 subview 的状态。相反,给它一个像 NSString *theStringMyLabelShouldShow 这样的属性,并从呈现的 vc 中设置它。第二个 VC 可以在 viewWillAppear:

中弄乱它自己的 View
self.name.text = self.theStringMyLabelShouldShow;

关于ios - 使用 addTarget : action: forControlEvents: 将 indexPath.row 移动到标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23850679/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com