Program Club

런타임에 UIBarButtonItem에 대한 대상 및 작업을 설정하는 방법

proclub 2020. 12. 6. 22:15
반응형

런타임에 UIBarButtonItem에 대한 대상 및 작업을 설정하는 방법


이것을 시도했지만 UIButton에서만 작동합니다.

[btn setTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];

UIBarButtonItem targetaction속성을 직접 설정하기 만하면 됩니다.


UIBarButtonItem에는 동일한 addTarget 메서드가 없으므로 다음과 같이 직접 설정해야합니다.

btn.target = self;
btn.action = @selector(barButtonCustomPressed:);

...

// can specify UIBarButtonItem instead of id for this case
-(IBAction)barButtonCustomPressed:(UIBarButtonItem*)btn 
{
    NSLog(@"button tapped %@", btn.title);
}

비슷한 문제가 발생했습니다 ... UIButton이 btnClicked를 호출하는 UITabBar의 일부가 아니라면 적절하게 작동한다는 것을 의미합니다. 이것이 제안하는 문제인 경우 btnClicked 메서드를 확인하고 다음과 같이 변경하십시오.

-btnClicked:(id)sender

...에

-(void) btnClicked:(id)sender

그리고 헤더 파일에서 btnClicked를 선언합니다.

그만한 가치는 tabbarbuttonitem에 버튼을 설정하는 방법입니다.

UIBarButtonItem *exampleButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"button.png"] style:UIBarButtonItemStylePlain target:self action:@selector(btnClicked:)];

당신의 세트 targetactionUIBarButtonItem

Swift 5 및 4

button.target = self
button.action = #selector(action)

@objc func action (sender:UIButton) {
    print("action")
}

코드에서이 시간이 충분히 필요하다면 UIBarButtonItemSwift에서 아래에서 수행 한 작업을 확장 하는 것이 좋습니다. :)

import UIKit

extension UIBarButtonItem {
    func addTargetForAction(target: AnyObject, action: Selector) {
        self.target = target
        self.action = action
    }
}

예를 들어 self를으로 사용하면 UIViewController다음과 같이 간단히 호출 할 수 있습니다.

self.myBarButtonItem.addTargetForAction(self, action: #selector(buttonPressed(_:))

  UIBarButtonItem *barListBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemAdd target:self action:@selector(getTruckStopListAction)];   
    self.navigationItem.rightBarButtonItem = barListBtn;
    [barListBtn release];

@ wp42 오늘 작동합니다.

Objective-C에서이를 수행하는 멋진 방법은 범주를 UIBarButtonItem 클래스에 추가하는 것입니다.

.h 파일

#import <UIKit/UIKit.h>

@interface UIBarButtonItem (addons)

-(void)addTarget:(id)target andAction:(SEL)action;

@end

.m 파일

#import "UIBarButtonItem+addons.h"

@implementation UIBarButtonItem (addons)

-(void)addTarget:(id)target andAction:(SEL)action{
   [self setTarget:target];
   [self setAction:action];
}

@end

실제로:

[myBtn addTarget:self andAction:@selector(myFunction:)];

프로그래밍 방식으로 UIBarButtonItem을 추가하는 경우 대상 및 작업을 설정하는 가장 좋은 방법은 다음 방법 중 하나로 버튼을 초기화하는 것입니다.

UIBarButtonItem *customButton = [[UIBarButtonItem alloc] initWithImage:<#(UIImage)#> style:<#(UIBarButtonItemStyle)#> target:<#(id)#> action:<#(SEL)#>

UIBarButtonItem *customButton = [UIBarButtonItem alloc] initWithTitle:<#(NSString *)#> style:<#(UIBarButtonItemStyle)#> target:<#(id)#> action:<#(SEL)#>

UIBarButtonItem *customButton = [UIBarButtonItem alloc] initWithImage:<#(UIImage *)#> landscapeImagePhone:<#(UIImage *)#> style:<#(UIBarButtonItemStyle)#> target:<#(id)#> action:<#(SEL)#>

addTarget 메서드를 사용해 볼 수 있습니다.

참고 URL : https://stackoverflow.com/questions/2333638/how-to-set-target-and-action-for-uibarbuttonitem-at-runtime

반응형