1

我一直在看类似的问题,但无法弄清楚问题出在哪里。它似乎应该可以工作,但它给了我错误。

在 IOS 5.1 Ipad Stortyboard 应用程序中,当用户单击弹出视图时,我有一个正确的导航栏项目。我有一个可以工作的弹出窗口视图,但设计不好,所以我用一个新的弹出窗口类替换了它,现在它给了我以下错误

-[UIButton view]: unrecognized selector sent to instance 0xa17ba80
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIButton view]: unrecognized selector sent to instance 0xa17ba80'

我尝试了以下功能,但到目前为止没有一个有效。当我更改代码时,它给了我类似的错误。

- (IBAction)setColorButtonTapped:(id)sender{
- (void)setColorButtonTapped:(id)sender{
- (IBAction)setColorButtonTapped:(id)sender forEvent:(UIEvent*)event {
- (void)setColorButtonTapped:(id)sender forEvent:(UIEvent*)event {

当然我已经改变了关于ibactionvoid的ti

[backButton2 addTarget:self action:@selector(setColorButtonTapped:) forControlEvents:UIControlEventTouchUpInside];

这是代码 my.h 文件

#import <UIKit/UIKit.h>
#import "ColorPickerController.h"

@interface MeetingViewController : UITableViewController<UIApplicationDelegate,UIAlertViewDelegate,DropDownListDelegate,MFMailComposeViewControllerDelegate,EGORefreshTableHeaderDelegate,ColorPickerDelegate>{

    UIPopoverController *_popover;
    ColorPickerController *_colorPicker;
    UIPopoverController *_colorPickerPopover;

}
@property (nonatomic, strong) UIPopoverController *popover;
@property (nonatomic, strong) ColorPickerController *colorPicker;
@property (nonatomic, strong) UIPopoverController *colorPickerPopover;

- (IBAction)setColorButtonTapped:(id)sender;
@end

我的.m 文件

@synthesize popover = _popover;
@synthesize colorPicker = _colorPicker;
@synthesize colorPickerPopover = _colorPickerPopover;

- (void)viewDidLoad 
{
    [super viewDidLoad];
    //gear button on navigation Bar
    UIImage* imageback2 = [UIImage imageNamed:@"ICON - Gear@2x.png"];
    CGRect frameimgback2 = CGRectMake(0, 0, 40, 40);

    UIButton *backButton2 = [[UIButton alloc] initWithFrame:frameimgback2];
    [backButton2 setBackgroundImage:imageback2 forState:UIControlStateNormal];
    [backButton2 addTarget:self
                    action:@selector(setColorButtonTapped:)
          forControlEvents:UIControlEventTouchUpInside];

    UIBarButtonItem *btn2 = [[UIBarButtonItem alloc] initWithCustomView:backButton2];
    self.navigationItem.rightBarButtonItem = btn2;

}
#pragma mark ColorPickerDelegate

- (void)colorSelected:(NSString *)color {

    [self.colorPickerPopover dismissPopoverAnimated:YES];
}

#pragma mark Callbacks

- (IBAction)setColorButtonTapped:(id)sender {
    if (_colorPicker == nil) {
        self.colorPicker = [[ColorPickerController alloc] initWithStyle:UITableViewStylePlain];
        _colorPicker.delegate = self;
        self.colorPickerPopover = [[UIPopoverController alloc] initWithContentViewController:_colorPicker];
    }
    [self.colorPickerPopover presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}

实用程序类 ColorPickerController.h

#import <UIKit/UIKit.h>

@protocol ColorPickerDelegate
- (void)colorSelected:(NSString *)color;
@end


@interface ColorPickerController : UITableViewController {
    NSMutableArray *_colors;
    id<ColorPickerDelegate> __weak _delegate;
}

@property (nonatomic, strong) NSMutableArray *colors;
@property (nonatomic, weak) id<ColorPickerDelegate> delegate;

@end

实用程序类 ColorPickerController.m

#import "ColorPickerController.h"


@implementation ColorPickerController
@synthesize colors = _colors;
@synthesize delegate = _delegate;

#pragma mark -
#pragma mark Initialization

/*
- (id)initWithStyle:(UITableViewStyle)style {
    // Override initWithStyle: if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
    if ((self = [super initWithStyle:style])) {
    }
    return self;
}
*/


#pragma mark -
#pragma mark View lifecycle


- (void)viewDidLoad {
    [super viewDidLoad];
    self.clearsSelectionOnViewWillAppear = NO;
    self.contentSizeForViewInPopover = CGSizeMake(150.0, 140.0);
    self.colors = [NSMutableArray array];
    [_colors addObject:@"Red"];
    [_colors addObject:@"Green"];
    [_colors addObject:@"Blue"];
}




- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Override to allow orientations other than the default portrait orientation.
    return YES;
}


#pragma mark -
#pragma mark Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return [_colors count];
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    // Configure the cell...
    NSString *color = [_colors objectAtIndex:indexPath.row];
    cell.textLabel.text = color;

    return cell;
}




#pragma mark -
#pragma mark Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (_delegate != nil) {
        NSString *color = [_colors objectAtIndex:indexPath.row];
        [_delegate colorSelected:color];
    }
}


#pragma mark -
#pragma mark Memory management

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Relinquish ownership any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
    // For example: self.myOutlet = nil;
}


- (void)dealloc {
    self.delegate = nil;
}

@end

非常感谢帮助,谢谢

4

2 回答 2

4

您正在将 aUIButton用作 a 的 customView UIBarButtonItem。这可能是问题所在。我建议您改用UIBarButtonItem'initWithImage:style:target:action:初始化程序。

于 2012-09-26T14:51:44.290 回答
0

UIButton 是一个视图,因此没有视图属性或实例方法。

于 2012-09-27T01:22:23.283 回答