1

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

第二个按钮,我有一个动作设置,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 中有一些注释掉的行。但这些都没有设置 的值testPopupViewController *popupVC = [segue destinationViewController];由于它引用 UINavigationController 引发错误,所以我不能按原样使用它,即使这似乎是我见过的很多答案中的方法。但无论我尝试哪种方式,输出总是为空?

更新:

PopupViewController *popupVC = (PopupViewController *)[[segue destinationViewController] visibleViewController];PopupViewController *popupVC=[[[segue destinationViewController]viewControllers]objectAtIndex:0];从我上面看,prepareForSegue两者都在 6.1 模拟器上工作。我的 iPad 的 iOS 是 5.1.1,它不工作。我需要为 iOS 5 做些什么不同的事情吗?

4

1 回答 1

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");
    }
}
于 2013-02-19T08:58:42.030 回答