0

我有一堂课RootViewController,我有一个UIBarButtonItem声明。显示它的方法在另一个类FirstDetailViewController中。

我试图在另一个类中访问它SecondDetailViewController,但它始终为空。我用其他一些变量进行了测试,它们也为空。这是我所拥有的:

RootViewController.h

@interface RootViewController : UITableViewController <UISplitViewControllerDelegate> {

}

@property (nonatomic, retain) UIBarButtonItem *rootPopoverButtonItem;
...
@end

根视图控制器.m

#import "RootViewController.h"
#import "FirstDetailViewController.h"

@implementation RootViewController

@synthesize popoverController, splitViewController, rootPopoverButtonItem;

- (void)splitViewController:(UISplitViewController*)svc willHideViewController:(UIViewController *)aViewController withBarButtonItem:(UIBarButtonItem*)barButtonItem forPopoverController:(UIPopoverController*)pc {

    NSLog(@"splitviewController will hide");

    // Keep references to the popover controller and the popover button, and tell the detail view controller to show the button.
    barButtonItem.title = @"Menu";
    self.popoverController = pc;
    self.rootPopoverButtonItem = barButtonItem;
    UIViewController <SubstitutableDetailViewController> *detailViewController = [splitViewController.viewControllers objectAtIndex:1];
    [detailViewController showRootPopoverButtonItem:rootPopoverButtonItem];
}

FirstDetailViewController.m

- (void)showRootPopoverButtonItem:(UIBarButtonItem *)barButtonItem {

    NSLog(@"show root popover button item");

    // Add the popover button to the toolbar.
    NSMutableArray *itemsArray = [toolbar.items mutableCopy];
    [itemsArray insertObject:barButtonItem atIndex:0];
    [toolbar setItems:itemsArray animated:NO];
    [itemsArray release];
}

SecondDetailViewController.h

    #import "RootViewController.h"

    @class RootViewController;

    @interface SecondDetailViewController : UIViewController <SubstitutableDetailViewController, UIScrollViewDelegate, UITextFieldDelegate, UITextViewDelegate> {
    ...
    }
    @property (nonatomic, retain) RootViewController *root;
    @end

SecondDetailViewController.m

#import "SecondDetailViewController.h"

@implementation SecondDetailViewController

@synthesize root;

...
NSLog(@"view class : %@", [root.splitViewController class]);
[detailViewController showRootPopoverButtonItem:root.rootPopoverButtonItem];
...
4

1 回答 1

0

您可能没有将 SecondDetailViewController 的“根”属性设置为要访问 UIBarButtonItem 的 RootViewController 实例。然后,您正在 SecondDetailViewController 代码中读取 RootViewController 的未初始化实例,而您没有收到错误的唯一原因是 Objective C 默默地忽略了对 nil 对象上的方法的调用(在本例中为 rootPopoverButtonItem getter 方法,root.rootPopoverButtonItem即的简写)。

如果您的“RootViewController”实例被称为“myRootViewController”,那么您必须在代码中的某处执行以下操作:

SecondDetailViewController *mySecondDetailViewController = [[SecondDetailViewController alloc] init];
mySecondDetailViewController.root = myRootViewController;

然后,您将访问具有所需栏按钮的 RootViewController 副本。

于 2012-07-19T21:08:14.117 回答