2012-12-02 66 views

回答

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"); 
} 
+0

很好的解释谢谢! – Tahlil

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]; 
+2

这对iOS..So它是UIControl的子类? ?对? –

相关问题