开发者

Problem with custom navigation bar that changes its background, plus custom back button

开发者 https://www.devze.com 2023-03-28 17:31 出处:网络
I have UINavigationBar subclassed and I add a custom background like so: @implementation CustomUINavigationBar

I have UINavigationBar subclassed and I add a custom background like so:

@implementation CustomUINavigationBar

@synthesize backImg;

- (void)drawRect:(CGRect)rect {
    if (backImg != nil) {
        [backImg drawInRect:CGRectMake(rect.origin.x, rect.origin.y, backImg.size.width, backImg.size.height)];
    }else{
        [super 开发者_如何学编程drawRect:rect];
    }
}

-(void)setBackImg:(UIImage *)nBackImg{
   backImg = nBackImg;
   if (backImg != nil) {
       [self setNeedsDisplay];
   }
}

setBackImg gets called in the app delegate and depends on which view controller I load in the navigationcontroller

In the view controller itself I do this:

- (void)viewDidLoad
{
    [super viewDidLoad];
    //Custom back button
    // Set the custom back button
    UIImage *buttonImage = [UIImage imageNamed:@"back.png"];

    //create the button and assign the image
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:buttonImage forState:UIControlStateNormal];

    //set the frame of the button to the size of the image (see note below)
    button.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height);

    [button addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];

    //create a UIBarButtonItem with the button as a custom view
    UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];

    self.navigationItem.leftBarButtonItem = customBarItem;

    [customBarItem release];
}

Now it does appear, but (sometimes) when I press on it or the view controller's content (always) leads to an uncaught exception:

'NSInvalidArgumentException', reason: '-[UIButton setTracking:]: unrecognized selector sent to instance 0x4f62840

ANSWER (can't answer my own question yet):

Turned out to be a bug in IOS5, fixed by downgrading. Will be filling a bug report.


Turned out to be a bug in IOS5, fixed by downgrading. Will be filling a bug report.


Because you are pass an invalid argument to the setTracking method(I do not know its your method or in-built), such as a nil pointer where a non-nil object is required. and the second reason could be you're not memory managing the view controller properly and it is being deallocated at some point - which causes the back: method to be sent to another object that is now occupying the memory that the view controller was previously occupying...

0

精彩评论

暂无评论...
验证码 换一张
取 消