IOS 5.1 ARC无法识别的选择器发送到实例

时间:2012-09-26 14:40:29

标签: objective-c ios error-handling uibutton popover

我一直在寻找类似的问题,但无法弄清楚问题是什么。它似乎应该有效,但它给了我错误。

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

-[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 {

当然我已经改变了 ibaction void

[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

my.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

utilityclass 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

非常感谢帮助,谢谢

2 个答案:

答案 0 :(得分:4)

您使用UIButton作为UIBarButtonItem的自定义视图。这可能是问题所在。 我建议您使用UIBarButtonItem的{​​{1}}初始值设定项。

答案 1 :(得分:0)

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