5

こんにちは私はプログラムでボタンを作成しました。このボタンをナビゲーションバーに追加します。次に、TouchUpInsideアクションリスナーを追加します。どうすればいいのですか?ありがとう。

4

2 に答える 2

16

UIButtonは、UIControlのサブクラスです。

ボタンを作成した後に行う必要があるのは、ボタンのターゲットとアクションを設定することだけです。すなわち

// Create your button:
UIButton *button = // However you create your button

// Set the target, action and event for the button
[button addTarget:// the object that implements the action method, or nil if you want it to propagate up the responder chain.
           action:// A selector for the method
 forControlEvents:UIControlEventTouchUpInside];
于 2012-12-02T12:02:53.767 に答える
6

それらをナビゲーションバーに追加したので、少し異なりますが、基本的に同じです。ボタンを作成すると同時に、リスナー/ハンドラーを追加します。ここでは、以下を使用してナビゲーションバーに<<とを追加しました。>>

UIBarButtonItem *nextButton = [[UIBarButtonItem alloc] initWithTitle:@">>" style:UIBarButtonItemStylePlain target:self action:@selector(navNextButtonPressed)];
UIBarButtonItem *prevButton = [[UIBarButtonItem alloc] initWithTitle:@"<<" style:UIBarButtonItemStylePlain target:self action:@selector(navPrevButtonPressed)];
self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:nextButton, prevButton, nil];

次に、通常どおりハンドラーを作成します。

#pragma mark - button handling
-(void)navNextButtonPressed
{    
    NSLog(@"Next pressed");
}

-(void)navPrevButtonPressed
{
    NSLog(@"Prev pressed");
}
于 2012-12-02T12:41:19.950 に答える