2010-02-16 60 views

回答

9
  1. 转到iPhone Interface Guidelines Page

  2. 在“用于表格行和其他用户界面元素的标准按钮”下复制ContactAdd按钮(我将它另存为ContactAdd.png)。将其添加到您的项目中。

  3. 在的cellForRowAtIndexPath:(NSIndexPath *)indexPath方法Add:

    UIImage *image = [UIImage imageNamed:@"ContactAdd.png"]; 
    
    
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 
    //You can also Use: 
    //UIButton *button = [UIButton buttonWithType:UIButtonTypeContactAdd]; 
    
    CGRect frame = CGRectMake(0.0, 0.0, image.size.width, image.size.height); 
    
    //match the button's size with the image size 
    button.frame = frame; 
    
    [button setBackgroundImage:image forState:UIControlStateNormal]; 
    
    // set the button's target to this table view controller so you can open the next view 
    [button addTarget:self action:@selector(yourFunctionToNextView:) forControlEvents:UIControlEventTouchUpInside]; 
    
    button.backgroundColor = [UIColor clearColor]; 
    
    cell.accessoryView = button; 
    
+12

* NOTE: 除了复制和添加,您还可以使用:[UIButton buttonWithType:UIButtonTypeContactAdd];而不是UIButtonTypeCustom。 – erastusnjuki 2010-02-16 13:36:19

+1

执行这些任务之一的棘手部分是将参数传递给yourFunctionToNextView:以便知道哪一行被按下。 这可以通过button.tag = row轻松完成; 但是,如果你的表中有一些部分,那么一个int就不够好。你需要传递indexPath。我在这里发现了这种方法来做这样的事情。 http://stackoverflow.com/questions/5500327/subclass-uibutton-to-add-a-property – roocell 2011-12-25 14:45:20

+0

传递参数是棘手的部分。您的链接几乎完成,除非它不告诉您如何传递添加到UIButton类的属性。为了完成这个想法,仅当在选择器中的“yourFunctionToNextView:”之后包含“:”时才会传递该参数。所以上面的例子可以通过button.property = indexPath来扩展。然后,选择器的定义是: - (void)yourFunctionToNextView:(UIButton *)sender {}。然后在这个方法中,你可以使用类似NSIndexPath * indexPath = sender.property;其中属性是indexPath。 – JeffB6688 2013-04-10 21:46:53

27

如果你有一个导航栏,你应该添加的UIBarButtonItem这样的:

UIBarButtonItem *addButton = [[UIBarButtonItem alloc]  
    initWithBarButtonSystemItem:UIBarButtonSystemItemAdd 
    target:self action:@selector(addButtonPressed:)]; 
self.navigationItem.rightBarButtonItem = addButton; 
+0

谢谢。帮助过我。 Upvote给你。 – DrinkJavaCodeJava 2014-03-23 18:15:40

2

对于斯威夫特

let addButton = UIBarButtonItem.init(barButtonSystemItem: .Add, 
            target: self, 
            action: #selector(yourFunction)) 
self.navigationItem.rightBarButtonItem = addButton 
相关问题