无法从另一个班级获得财产

时间:2012-07-19 18:05:25

标签: objective-c uiviewcontroller uibutton

我有一个班级RootViewController,我宣布了UIBarButtonItem。显示它的方法是在另一个类FirstDetailViewController

我正在尝试在另一个类SecondDetailViewController中访问它,但它始终为null。我测试了一些其他变量,它们也是null。这就是我所拥有的:

RootViewController.h

@interface RootViewController : UITableViewController <UISplitViewControllerDelegate> {

}

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

RootViewController.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];
...

1 个答案:

答案 0 :(得分:0)

您可能没有将SecondDetailViewController的“root”属性设置为要访问UIBarButtonItem的RootViewController实例。然后你在SecondDetailViewController代码中读取一个未初始化的RootViewController实例,并且你没有得到错误的唯一原因是Objective C默默地忽略对nil对象上方法的调用(在这种情况下是rootPopoverButtonItem getter方法,{ {1}}是简写)。

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

root.rootPopoverButtonItem

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