值不发送到下一个UIViewController

时间:2013-11-23 09:15:29

标签: ios button

有人能告诉我为什么我的值没有从UIViewController发送到UITableViewController吗?

在我的UIViewController中,我有一些以编程方式创建的按钮,每个按钮都有自己的标签编号,如下所示:

bySeaButton = [UIButton buttonWithType:UIButtonTypeCustom];
bySeaButton.tag = 4;
bySeaButton.frame = CGRectMake(20, 294, 280.f, 40.f);
UIImage *seaButton = [UIImage imageNamed:@"gettingHereBySeaButton.png"];
[bySeaButton setBackgroundImage:seaButton forState:UIControlStateNormal];
[self.view addSubview:bySeaButton];
[bySeaButton addTarget:self action:@selector(bySeaButtonClicked) forControlEvents:UIControlEventTouchUpInside];

然后在我的prepareForSegue中,我将我的UITableViewController中的tagVale设置为按钮标签号,如下所示:

- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    MyTableViewController *vc = [[MyTableViewController alloc] init];
    vc.tagNumber = bySeaButton.tag;
}

此处的NSLog显示vc.tagNumber设置为按钮标签号。但是,当我在UITableViewController中访问tagNumber的值时,它总是设置为0.以下是我如何使用它(tagNumber在UITableViewController的.m中声明为@property(nonatomic,assign)int tagNumber;)

 NSString *strFromInt = [NSString stringWithFormat:@"%d", self.tagNumber];

此输出始终为0 - 无论按钮标记值

如何

2 个答案:

答案 0 :(得分:1)

在preparesegue中尝试此代码

- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    {

        MyTableViewController *vc = segue.destinationViewController;

        vc.tagNumber = bySeaButton.tag;
    }

答案 1 :(得分:0)

在目标

中添加此方法后,首先在storyboard中声明segue id
[self performSegueWithIdentifier:@"push segue id" sender:self];


- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    {
        if ([[segue identifier] isEqualToString:@"push segue id"])
    {
        MyTableViewController *vc = [segue   destinationViewControlle];

        vc.tagNumber = bySeaButton.tag;
    }
   }