UIBarButtonItem을 생성하려고 할 때, 초기화메서드들을 살펴보면
UIImage* image = [UIImage imageNamed:@"btn_left.png"];
– initWithBarButtonSystemItem:target:action:
– initWithCustomView:
– initWithImage:style:target:action:
– initWithTitle:style:target:action:
이렇게 네가지가있습니다. CustomView를 사용할 때만 target: action:을 설정하는 부분이 빠져있지요.
뷰를 붙여넣었기 때문입니다. UIView는 UIControl의 상위 클래스지요.
그러므로 Action을 설정하고 싶으면 CustomView의 인자로 UIButton을 사용하면 됩니다.
그리고 UIButton에 Action을 설정해 넣으면 됩니다.
UIImage* image = [UIImage imageNamed:@"btn_left.png"];
CGRect frame = CGRectMake(0, 0, image.size.width, image.size.height);
UIButton* someButton = [[UIButton alloc] initWithFrame:frame];
[someButton setBackgroundImage:image forState:UIControlStateNormal];
UIImage* image1 = [UIImage imageNamed:@"btn_leftTouch.png"];
[someButton setBackgroundImage:image1 forState:UIControlStateHighlighted];
[someButton setShowsTouchWhenHighlighted:NO];
UIBarButtonItem* someBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:someButton];
UIImage* imager = [UIImage imageNamed:@"btn_right.png"];
CGRect framer = CGRectMake(0, 0, imager.size.width, imager.size.height);
UIButton* someButtonr = [[UIButton alloc] initWithFrame:framer];
[someButtonr setBackgroundImage:imager forState:UIControlStateNormal];
UIImage* imager1 = [UIImage imageNamed:@"btn_rightTouch.png"];
[someButtonr setBackgroundImage:imager1 forState:UIControlStateHighlighted];
[someButtonr setShowsTouchWhenHighlighted:NO];
UIBarButtonItem* someBarButtonItemr = [[UIBarButtonItem alloc] initWithCustomView:someButtonr];
UIBarButtonItem* flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
[toolBar setItems:[NSArray arrayWithObjects:flexibleSpace, someBarButtonItem,flexibleSpace, someBarButtonItemr, flexibleSpace, nil]];
댓글 없음:
댓글 쓰기