使用弹出窗口在prepareForSegue中设置属性

时间:2013-02-19 08:40:21

标签: ios objective-c

我有一个ViewController,它在Interface Builder中创建了几个按钮。第一个按钮将显示在IB中链接的弹出窗口,它链接到UINavigationController并且在其下面有一个类PopupViewController的TableView。

第二个按钮,我有一个goToCategory的操作设置,当点击它时,我想在PopupViewController上设置一个属性

ViewController.m
//go to category
-(IBAction)goToCategory:(id)sender{
    NSLog(@"GO TO CAT");
    //PopupViewController *popupVC = [self.storyboard instantiateViewControllerWithIdentifier:@"popoverVC"];
    //popupVC.currentLevel = 1;
    [self performSegueWithIdentifier:@"popoverSegue" sender:self];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    NSLog(@"seg1");
    if ([[segue identifier] isEqualToString:@"popoverSegue"]){
        //PopupViewController *popupVC = (PopupViewController *)[[segue destinationViewController] visibleViewController];
        //PopupViewController *popupVC = [segue destinationViewController];
        PopupViewController *popupVC=[[[segue destinationViewController]viewControllers]objectAtIndex:0];
        popupVC.test = @"just a test";
        NSLog(@"seg2");
    }
}

PopupViewController.h
@property (nonatomic, strong) NSString *test;

PopupViewController.m
@synthesize test;
-(void)viewDidLoad{
    NSLog(@"test: %@", test); //returns test: (null)
}

我在SO上找到了很多答案,因此我的prepareForSegue中有一些注释掉的行。但这些都没有设定test的值。 PopupViewController *popupVC = [segue destinationViewController];引发错误,因为它引用了UINavigationController,因此我无法使用它,即使这似乎是我已经看到的很多答案中的方法。但无论我尝试哪种方式,输出始终为空?

更新

上面我PopupViewController *popupVC = (PopupViewController *)[[segue destinationViewController] visibleViewController];的{​​p> PopupViewController *popupVC=[[[segue destinationViewController]viewControllers]objectAtIndex:0];prepareForSegue都在6.1模拟器上工作。我的iPad的iOS是5.1.1,它没有用。我需要为iOS 5做些什么吗?

1 个答案:

答案 0 :(得分:0)

试试这个

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    NSLog(@"seg1");
    if ([[segue identifier] isEqualToString:@"popoverSegue"])
    {
        UINavigationController *navController = (UINavigationController*)[segue destinationViewController];
        PopupViewController *popupVC = [navController topViewController];
        popupVC.test = @"just a test";
        NSLog(@"seg2");
    }
}