2012-07-17 2 views
0
- (void)viewDidLoad 
{ 

    [super viewDidLoad]; 

    //Load the image 
    UIImage *buttonImage = [UIImage imageNamed:@"1.png"]; 

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

    //sets the frame of the button to the size of the image 
    button.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height); 

    //creates a UIBarButtonItem with the button as a custom view 
    UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button]; 
    customBarItem.target = self; 
    customBarItem.action = @selector(click:); 

    self.toolbarItems = [NSArray arrayWithObjects:customBarItem, nil]; 
} 

- (void)click:(id)sender { 
    NSLog(@"action"); 
} 

barItem을 눌렀을 때 콘솔에 "action"이 출력 될 것 같습니다. 그러나 "작업"이 인쇄되지 않습니다. 나는 무엇이 있 었는가? 미리 감사드립니다.UIBarButtonItem target-action이 호출되지 않습니다.

답변

1

난 당신이 단순히 UIView을 통과해야 할 때 [[UIBarButtonItem alloc] initWithCustomView에 대한 UIButton을 사용하고 있는지 문제가 생각하십시오.

대신 이렇게 :

UIImageView *myImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1.png"]; 
UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:myImageView]; 
2

는 작업이 아마도 UIButton 아닌 UIBarButtonItem에 가고,이

UIBarButtonItem *btn = [[UIBarButtonItem alloc] initWithTitle:@"Click ME!" style:UIBarButtonItemStyleBordered target:self action:@selector(click)]; 

. 
. 
. 

-(void)click{ 
    NSLog("hello"); 
} 
1

을 당신이 더 같은 일을하려는 생각 :

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 
[button setImage:buttonImage forState:UIControlStateNormal]; 
[button setFrame:CGRectMake(0.0f, 0.0f, 25.0f, 25.0f)]; 
[button addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside]; 
UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button]; 
관련 문제