将参数传递给新的viewcontroller

时间:2012-10-22 11:21:17

标签: iphone ios xcode segue

当我想要一个新的viewController时,想要传递一个参数,让我们说“turnId”到一个新的ViewController。我知道我可以在prepareForSegue方法中执行此操作,但是如何将参数传递给prepareForSegue方法本身?我如何从发件人那里得到这个?发件人为didSelectRowAtIndexPath,但该值不在单元格中,而只是在didSelectRowAtIndexPath中声明,如:

String *turnId = @"123";

1 个答案:

答案 0 :(得分:3)

我不会在发件人信息中传递它。

didSelectRowAtIndexPath中执行[self performSegueWithIdentifire:@"blah"],然后在prepareForSegue执行此类操作...

- (void)prepareForSegue:... //blah cant remember the name
{
    if ([segue.identifier isEqualToString:@"blah"]) {
        NewViewController *controller = segue.destinationViewController;

        NSIndexPath *selectedIndexPath = [self.tableView indexPathForSelectedRow];

        //get the object associated with that row and set a variable like...
        NSString *selectedString = tableObject.name;

        //Then pass the string into the controller...
        controller.stringProperty = selectedString;
    }
}

然后在NewViewController.h设置@property以接受此...

@property (nonatomic, strong) NSString *stringProperty;

然后您可以从NewViewCongtroller.m访问此值,并将值传递给它。