UIViewController 可能无法响应 showRootPopoverButtonItem

发布于 2025-01-03 14:45:03 字数 7745 浏览 1 评论 0原文

我正在使用Apple的MuiltipleDetailViewController示例应用程序并收到消息“UIViewController可能不会响应showRootPopoverButtonItem”

这在XCode 3.X中有效,但我在4.2中收到消息

应用程序本身功能100%,弹出窗口在每个笔尖中都被识别,如是横向模式下左侧的表格。但我无法带着这个警告提交。我需要改变什么??

RootViewController.h

#import <UIKit/UIKit.h>


/*
 SubstitutableDetailViewController defines the protocol that detail view controllers must adopt. The protocol specifies methods to hide and show the bar button item controlling the popover.

 */
@protocol SubstitutableDetailViewController <NSObject>
- (void)showRootPopoverButtonItem:(UIBarButtonItem *)barButtonItem;
- (void)invalidateRootPopoverButtonItem:(UIBarButtonItem *)barButtonItem;
@end


@interface RootViewController : UITableViewController <UISplitViewControllerDelegate> {

    UISplitViewController *splitViewController;

    UIPopoverController *popoverController;    
    UIBarButtonItem *rootPopoverButtonItem;

    //UINavigationBar *navigationBar;
}

@property (nonatomic, assign) IBOutlet UISplitViewController *splitViewController;

@property (nonatomic, retain) UIPopoverController *popoverController;
@property (nonatomic, retain) UIBarButtonItem *rootPopoverButtonItem;


//@property (nonatomic, retain) IBOutlet UINavigationBar *navigationBar;

@end

RootViewController.m:

#import "RootViewController.h"
#import "WebViewController.h"
#import "Twitter.h"
//#import "SubstitutableDetailViewController.h"



@implementation RootViewController

@synthesize splitViewController, popoverController, rootPopoverButtonItem;//, navigationBar;


#pragma mark -
#pragma mark View lifecycle

- (void)viewDidLoad {

    [super viewDidLoad];
    // Set the content size for the popover: there are just two rows in the table view, so set to rowHeight*2.
    self.contentSizeForViewInPopover = CGSizeMake(310.0, self.tableView.rowHeight*2.0);
    //self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:255/255 green:104/255 blue:1/255 alpha:1];
}
/*
-(void)customizeAppearance {
    //create resizable images
    UIImage *bluImage = [UIImage imageNamed:@"blu.jpg"];// resizableImageWithCapInsets:(0, 0, 0, 0)];

    //set the bg for *all* UINavBars
    [[UINavigationBar appearance] setBackgroundImage:bluImage forBarMetrics:UIBarMetricsDefault];
}
 */

-(void) viewDidUnload {
    [super viewDidUnload];

    self.splitViewController = nil;
    self.rootPopoverButtonItem = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return YES;
}


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

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


- (void)splitViewController:(UISplitViewController*)svc willShowViewController:(UIViewController *)aViewController 
  invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem {

    // Nil out references to the popover controller and the popover button, and tell the detail view controller to hide the button.
    UIViewController <SubstitutableDetailViewController> *detailViewController = [splitViewController.viewControllers objectAtIndex:1];
    [detailViewController invalidateRootPopoverButtonItem:rootPopoverButtonItem];
    self.popoverController = nil;
    self.rootPopoverButtonItem = nil;
}


#pragma mark -
#pragma mark Table view data source

- (NSInteger)tableView:(UITableView *)aTableView numberOfRowsInSection:(NSInteger)section {

    // Two sections, one for each detail view controller.
    return 2;
}


- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"RootViewControllerCellIdentifier";

    // Dequeue or create a cell of the appropriate type.
    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        //cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    }

    // Set appropriate labels for the cells.
    if (indexPath.row == 0) {
        cell.textLabel.text = @"Twitter";
    }

    else if (indexPath.row == 1) {
        cell.textLabel.text = @"Contact Us";
    }


    cell.textLabel.textColor = [UIColor whiteColor];
    cell.textLabel.backgroundColor = [UIColor blackColor];
    cell.contentView.backgroundColor = [UIColor blackColor];
    cell.detailTextLabel.backgroundColor = [UIColor blackColor];
    return cell;
}


#pragma mark -
#pragma mark Table view selection

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    /*
     Create and configure a new detail view controller appropriate for the selection.
     */
    NSUInteger row = indexPath.row;

    UIViewController *detailViewController = nil;

    if (row == 0) {
        Twitter *newDetailViewController = [[Twitter alloc] 
                                                      initWithNibName:@"Twitter" 
                                                      bundle:nil];
        detailViewController = newDetailViewController;
    }    

    if (row == 1) {
        WebViewController *newDetailViewController = [[WebViewController alloc] 
                                                      initWithNibName:@"WebViewController" 
                                                      bundle:nil];
        newDetailViewController.detailURL=
        [[NSURL alloc] initWithString:@"http://www.chipmunkmobile.com/contact.html"];

        detailViewController = newDetailViewController;
    }   

    // Update the split view controller's view controllers array.
    NSArray *viewControllers = [[NSArray alloc] initWithObjects:self.navigationController, detailViewController, nil];
    splitViewController.viewControllers = viewControllers;
    [viewControllers release];

    // Dismiss the popover if it's present.
    if (popoverController != nil) {
        [popoverController dismissPopoverAnimated:YES];
    }

    // Configure the new view controller's popover button (after the view has been displayed and its toolbar/navigation bar has been created).
    if (rootPopoverButtonItem != nil) {
        [detailViewController showRootPopoverButtonItem:self.rootPopoverButtonItem];
    }


    [detailViewController release];
}

#pragma mark -
#pragma mark Managing the popover


/*
- (void)showRootPopoverButtonItem:(UIBarButtonItem *)barButtonItem {
    // Add the popover button to the left navigation item.
    [navigationBar.topItem setLeftBarButtonItem:barButtonItem animated:NO];
}


- (void)invalidateRootPopoverButtonItem:(UIBarButtonItem *)barButtonItem {
    // Remove the popover button.
    [navigationBar.topItem setLeftBarButtonItem:nil animated:NO];
}
 */

#pragma mark -
#pragma mark Memory management

- (void)dealloc {
    [popoverController release];
    [rootPopoverButtonItem release];
    [super dealloc];
}

@end

这是我收到警告的确切行的图像 在此处输入图像描述

I am using Apple's MuiltipleDetailViewController sample app and get the message "UIViewController may not respond to showRootPopoverButtonItem"

This worked in XCode 3.X, but I get the message with 4.2

The app itself functions 100%, the popover is recognized in every nib, as is the table on the left when in landscape mode. But I can't submit with this warning. What do I need to change??

RootViewController.h

#import <UIKit/UIKit.h>


/*
 SubstitutableDetailViewController defines the protocol that detail view controllers must adopt. The protocol specifies methods to hide and show the bar button item controlling the popover.

 */
@protocol SubstitutableDetailViewController <NSObject>
- (void)showRootPopoverButtonItem:(UIBarButtonItem *)barButtonItem;
- (void)invalidateRootPopoverButtonItem:(UIBarButtonItem *)barButtonItem;
@end


@interface RootViewController : UITableViewController <UISplitViewControllerDelegate> {

    UISplitViewController *splitViewController;

    UIPopoverController *popoverController;    
    UIBarButtonItem *rootPopoverButtonItem;

    //UINavigationBar *navigationBar;
}

@property (nonatomic, assign) IBOutlet UISplitViewController *splitViewController;

@property (nonatomic, retain) UIPopoverController *popoverController;
@property (nonatomic, retain) UIBarButtonItem *rootPopoverButtonItem;


//@property (nonatomic, retain) IBOutlet UINavigationBar *navigationBar;

@end

RootViewController.m:

#import "RootViewController.h"
#import "WebViewController.h"
#import "Twitter.h"
//#import "SubstitutableDetailViewController.h"



@implementation RootViewController

@synthesize splitViewController, popoverController, rootPopoverButtonItem;//, navigationBar;


#pragma mark -
#pragma mark View lifecycle

- (void)viewDidLoad {

    [super viewDidLoad];
    // Set the content size for the popover: there are just two rows in the table view, so set to rowHeight*2.
    self.contentSizeForViewInPopover = CGSizeMake(310.0, self.tableView.rowHeight*2.0);
    //self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:255/255 green:104/255 blue:1/255 alpha:1];
}
/*
-(void)customizeAppearance {
    //create resizable images
    UIImage *bluImage = [UIImage imageNamed:@"blu.jpg"];// resizableImageWithCapInsets:(0, 0, 0, 0)];

    //set the bg for *all* UINavBars
    [[UINavigationBar appearance] setBackgroundImage:bluImage forBarMetrics:UIBarMetricsDefault];
}
 */

-(void) viewDidUnload {
    [super viewDidUnload];

    self.splitViewController = nil;
    self.rootPopoverButtonItem = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return YES;
}


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

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


- (void)splitViewController:(UISplitViewController*)svc willShowViewController:(UIViewController *)aViewController 
  invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem {

    // Nil out references to the popover controller and the popover button, and tell the detail view controller to hide the button.
    UIViewController <SubstitutableDetailViewController> *detailViewController = [splitViewController.viewControllers objectAtIndex:1];
    [detailViewController invalidateRootPopoverButtonItem:rootPopoverButtonItem];
    self.popoverController = nil;
    self.rootPopoverButtonItem = nil;
}


#pragma mark -
#pragma mark Table view data source

- (NSInteger)tableView:(UITableView *)aTableView numberOfRowsInSection:(NSInteger)section {

    // Two sections, one for each detail view controller.
    return 2;
}


- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"RootViewControllerCellIdentifier";

    // Dequeue or create a cell of the appropriate type.
    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        //cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    }

    // Set appropriate labels for the cells.
    if (indexPath.row == 0) {
        cell.textLabel.text = @"Twitter";
    }

    else if (indexPath.row == 1) {
        cell.textLabel.text = @"Contact Us";
    }


    cell.textLabel.textColor = [UIColor whiteColor];
    cell.textLabel.backgroundColor = [UIColor blackColor];
    cell.contentView.backgroundColor = [UIColor blackColor];
    cell.detailTextLabel.backgroundColor = [UIColor blackColor];
    return cell;
}


#pragma mark -
#pragma mark Table view selection

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    /*
     Create and configure a new detail view controller appropriate for the selection.
     */
    NSUInteger row = indexPath.row;

    UIViewController *detailViewController = nil;

    if (row == 0) {
        Twitter *newDetailViewController = [[Twitter alloc] 
                                                      initWithNibName:@"Twitter" 
                                                      bundle:nil];
        detailViewController = newDetailViewController;
    }    

    if (row == 1) {
        WebViewController *newDetailViewController = [[WebViewController alloc] 
                                                      initWithNibName:@"WebViewController" 
                                                      bundle:nil];
        newDetailViewController.detailURL=
        [[NSURL alloc] initWithString:@"http://www.chipmunkmobile.com/contact.html"];

        detailViewController = newDetailViewController;
    }   

    // Update the split view controller's view controllers array.
    NSArray *viewControllers = [[NSArray alloc] initWithObjects:self.navigationController, detailViewController, nil];
    splitViewController.viewControllers = viewControllers;
    [viewControllers release];

    // Dismiss the popover if it's present.
    if (popoverController != nil) {
        [popoverController dismissPopoverAnimated:YES];
    }

    // Configure the new view controller's popover button (after the view has been displayed and its toolbar/navigation bar has been created).
    if (rootPopoverButtonItem != nil) {
        [detailViewController showRootPopoverButtonItem:self.rootPopoverButtonItem];
    }


    [detailViewController release];
}

#pragma mark -
#pragma mark Managing the popover


/*
- (void)showRootPopoverButtonItem:(UIBarButtonItem *)barButtonItem {
    // Add the popover button to the left navigation item.
    [navigationBar.topItem setLeftBarButtonItem:barButtonItem animated:NO];
}


- (void)invalidateRootPopoverButtonItem:(UIBarButtonItem *)barButtonItem {
    // Remove the popover button.
    [navigationBar.topItem setLeftBarButtonItem:nil animated:NO];
}
 */

#pragma mark -
#pragma mark Memory management

- (void)dealloc {
    [popoverController release];
    [rootPopoverButtonItem release];
    [super dealloc];
}

@end

Here is an image of the exact line where I get the warning
enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

ぽ尐不点ル 2025-01-10 14:45:03

您收到此警告的原因是因为此 UIViewController *detailViewController UIViewController 没有名为“showRootPopoverButtonItem”的方法。如果你想摆脱警告,只需这样做:

[(WebViewController*)detailViewController showRootPopoverButtonItem:self.rootPopoverButtonItem];

或者

[(Twitter*)detailViewController showRootPopoverButtonItem:self.rootPopoverButtonItem];

你只需​​要让它知道它不仅仅是一个viewController,它是你创建的一个子类viewController。因此,无论 showRootPopoverButtonItem: 类是什么,您只需要键入它即可。

The reason you getting this warning is because of this UIViewController *detailViewController UIViewController does not have a method called "showRootPopoverButtonItem". If you want to get rid of the warning just do this instead:

[(WebViewController*)detailViewController showRootPopoverButtonItem:self.rootPopoverButtonItem];

or

[(Twitter*)detailViewController showRootPopoverButtonItem:self.rootPopoverButtonItem];

You just need to let it know its not really just a viewController, its a subclassed viewController you created. So what ever class showRootPopoverButtonItem: is in you just need to type cast it.

娜些时光,永不杰束 2025-01-10 14:45:03

如果您想让您的代码完全相同但消除警告,您可以这样做。

    if (rootPopoverButtonItem != nil) {
        [detailViewController performSelector:@selector(showRootPopoverButtonItem:) withObject:self.rootPopoverButtonItem];
    }

如果你想更加小心,你应该使用这个。

 if (rootPopoverButtonItem != nil && [detailViewController respondsToSelector:@selector(showRootPopoverButtonItem:)]) {
        [detailViewController performSelector:@selector(showRootPopoverButtonItem:) withObject:self.rootPopoverButtonItem];
    }

更新

阅读完代码后,您可以像在其他函数中一样在detailViewController上指定委托。

UIViewController<SubstitutableDetailViewController> *detailViewController = nil;

    if (row == 0) {
    //...

If you want to leave your code exactly the same but get rid of the warning you can do this.

    if (rootPopoverButtonItem != nil) {
        [detailViewController performSelector:@selector(showRootPopoverButtonItem:) withObject:self.rootPopoverButtonItem];
    }

If you want to be more careful you should use this.

 if (rootPopoverButtonItem != nil && [detailViewController respondsToSelector:@selector(showRootPopoverButtonItem:)]) {
        [detailViewController performSelector:@selector(showRootPopoverButtonItem:) withObject:self.rootPopoverButtonItem];
    }

Update

After reading through your code you could just specify the delegate on the detailViewController like you did in the other functions.

UIViewController<SubstitutableDetailViewController> *detailViewController = nil;

    if (row == 0) {
    //...
゛清羽墨安 2025-01-10 14:45:03

(1)指定detailedViewController的Type: [(TYPE *)OBJECTNAME...

(2) 然后指定方法调用 [(TYPE *)OBJECTNAME METHODCALL];

(*) 您收到警告是因为编译器不知道您正在使用什么类型的对象。如果您子类化 UIViewController 那么您必须在访问方法时指定类型。确保该方法位于 .h 中,以便您可以访问它。

(1)Specify the Type for the detailedViewController: [(TYPE *)OBJECTNAME...

(2) Then Specify method call [(TYPE *)OBJECTNAME METHODCALL];

(*) You get the warning because the compiler does not know what type of object you are using. If you subclass a UIViewController then you have to specify type when accessing methods.Make sure the method is in .h so that you can access it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文