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

时间:2014-05-24 23:40:10

标签: ios objective-c uibutton

我正在尝试将indexPath.row移动到另一个viewcontroller - 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

由于names[theButton.tag]因为什么而崩溃?

1 个答案:

答案 0 :(得分:1)

这一行:

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

创建一个新的视图控制器,然后这一行:

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

设置了其中一个视图的属性(这是错误的,因为视图尚未加载,并且因为它不明智地与另一个vc混乱#&# 39;观点)。但这并不重要,因为这一行:

}

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

修复方法是使用起始视图控制器中的segue,在prepareForSegue:中设置目标属性,或者推送或显示第二个ViewController。之一:

// 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,都不要尝试直接设置其中一个子视图的状态。相反,给它一个像NSString *theStringMyLabelShouldShow这样的属性,并从呈现的vc中设置它。第二个VC可能会在viewWillAppear:

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