2012-05-11 42 views
7

我已经创建了一个具有自己的.m,.h和.xib文件的自定义单元格。在单元格中,我有一个UIButton,我在IB中添加到xib中。在主视图控制器的自定义单元格内使用UIButton的IBAction

我可以从这个自定义单元格的.m中的UIButton接收IBAction,但是真的,我想要将该按钮按下按钮转移到托管表格(以及自定义单元格)的主视图.m,并在那里采取行动。

我已经花了最后4小时尝试各种方式做到这一点 - 我应该使用NSNotificationCenter吗? (我试过很多通知,但不能让它工作,不知道我是否应该坚持就是胜利)

+0

我觉得NSNotificationCenter是一个合理的策略。您没有收到按钮的操作方法发送或其他问题的通知吗? –

+0

我也这么认为 - 我不认为我有实施他们的诀窍呢......更多的实践。我现在可以让他们在相同的视图下工作,但是当我尝试不同的视图时仍然会出现异常......我们会到达 –

+0

这是关于创建和使用委托协议的非常完整的解释。 http://www.dosomethinghere.com/2009/07/18/setting-up-a-delegate-in-the-iphone-sdk/ – MystikSpiral

回答

9

您需要在单元格的.h文件中使用委托。 声明这样

@class MyCustomCell; 
@protocol MyCustomCellDelegate 
- (void) customCell:(MyCustomCell *)cell button1Pressed:(UIButton *)btn; 
@end 

委托然后在.m文件

@synthesize delegate; 

和在按钮方法

- (void) buttonPressed { 
    if (delegate && [delegate respondToSelector:@selector(customCell: button1Pressed:)]) { 
     [delegate customCell:self button1Pressed:button]; 
    } 
} 

你的视图控制器声明字段和属性

@interface MyCustomCell:UItableViewCell { 
    id<MyCustomCellDelegate> delegate; 
} 

@property (nonatomic, assign) id<MyCustomCellDelegate> delegate; 

@end 

亩ST采用这个协议就像在cellForRow .m文件本

.h文件中

#import "MyCustomCell.h" 

@interface MyViewController:UIViewController <MyCustomCellDelegate> 
..... 
..... 
@end 

:你需要方法添加财产委托给细胞

cell.delegate = self; 

最后你实现从协议的方法

- (void) customCell:(MyCustomCell *)cell button1Pressed:(UIButton *)btn { 

} 

对不起,我的英语和代码。在没有XCODE的情况下从我的电脑写下它

+0

- (无效)buttonPressed { 如果(委托&& [委托respondToSelector:@selector(customCell:button1Pressed :)]){ [委托customCell:自button1Pressed:按钮]; }} 你在哪里得到的 “按钮”[代表customCell:自button1Pressed:按钮]。并且当我在控制器中添加方法时,将呈现另一个视图控制器的示例代码是什么?谢谢 – zramled

+0

要呈现另一个视图控制器,您可以使用[self presentViewController:animated:completion:];或[self.navigationController pushViewController:动画:] – Moonkid

+0

@Moonkid有没有一个迅速的答案呢? –

0

你可能只想在按钮添加选择你的视图控制器有你的tableview。

在cellForIndexPath功能

[yourCell.button addTarget:self action:@selector(customActionPressed:) forControlEvents:UIControlEventTouchDown]; 

然后处理按钮按下你的“customActionPressed:(ID)发送”方法

//Get the superview from this button which will be our cell 
UITableViewCell *owningCell = (UITableViewCell*)[sender superview]; 

//From the cell get its index path. 
NSIndexPath *pathToCell = [myTableView indexPathForCell:owningCell]; 

    //Do something with our path 

这可能是你更好的解决方案,除非有更多的你还没有列出的因素。

我有一个教程,是可以解释更多http://www.roostersoftstudios.com/2011/05/01/iphone-custom-button-within-a-uitableviewcell/

+0

谢谢,我已经知道按钮的路径了,因为我使用的方法是使用被选中的单元格来选择plist(有点俗气,但工作正常)。尽管我会尝试你的教程,看看我能否以不同的方式来解决我的问题。 –

+0

听起来不错。如果您知道索引并且可以以某种方式使用它来获取所需的数据,那么您可能需要在拥有的视图控制器中执行操作。祝你好运 – rooster117

+0

另外看看其他人使用委托的建议也是一个好主意。这是我前一段时间制作的另一个教程:[link] http://www.roostersoftstudios.com/2011/04/12/simple-delegate-tutorial-for-ios-development/ – rooster117

0

为什么不为您的自定义单元格的委托(使用@protocol)。然后,您可以将主视图指定为每个单元格的委托并适当地处理该操作。

+0

我想我最好研究一下代表......我不知道该怎么做! –

1

我遇到了同样的问题,我通过将单元格继承到它自己的类中,并将按钮作为插座并使用模型中的数据填充单元格,同时使用一种返回当前正在查看的单元格的方法。

举例来说,如果你有一个Person类,每个人有一个名字,姓氏,和一些朋友。而且每次在单元格中点击一个按钮时,朋友对一个具体的人的数量将增加1

_______________DATA SOURCE___________________________ 
#import <Foundation/Foundation.h> 

@interface Person : NSObject 

@property (nonatomic, strong) NSString *name; 
@property (nonatomic, strong) NSString *comment; 
@property (nonatomic) NSInteger numberOfFriends; 


+(instancetype)personWithName:(NSString *)aName Surname:(NSString *)aSurname; 

@end 

#import "Person.h" 

@implementation Person 

+(instancetype)personWithName:(NSString *)aName Surname:(NSString *)aSurname{ 

    Person *person = [[Person alloc] init]; 
    [person setName:aName]; 
    [person setSurname:aSurname]; 
    [person setNumberOfFriends:0]; 

    return person; 
} 

@end 

_____________________PERSON CELL________________________ 

#import <UIKit/UIKit.h> 

@interface PersonCell : UITableViewCell 

@property (strong, nonatomic) IBOutlet UILabel *friendsNum; 
@property (strong, nonatomic) IBOutlet UIButton *friendsBtn; 
@property (strong, nonatomic) IBOutlet UILabel *nameLabel; 
@property (strong, nonatomic) IBOutlet UILabel *surnameLabel; 


@end 

个人而言,我创建了一个私人的NSArray持有我一个人对象的名称和一个私人的NSMutableDictionary增加来保存我的Person对象,并且我将这些键设置为人员的名字。

_____________________PERSON TABLE VIEW________________________  
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    PersonCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 
    NSString *name = [peopleNames objectAtIndex:indexPath.row]; 
    Person *person = [people objectForKey:name]; 

    if(cell == nil) 
    { 
     cell = [[PersonCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 

    // Configure the cell... 
    cell.nameLabel.text = person.name; 
    cell.surname.Label.text = person.surname 

    [cell.friendsButton addTarget:self action:@selector(moreFriends:) forControlEvents:UIControlEventTouchUpInside]; 

    cell.friendsNum.text = [NSString stringWithFormat:@"%i", person.numberOfFriends]; 

    return cell; 
} 

- (IBAction)moreFriends:(id)sender { 
    UIButton *btn = (UIButton *)sender; 
    PersonCell *cell = [self parentCellForView:btn]; 
    Person *person = [people objectForKey:cell.nameLabel.text]; 
    person.numberOfFriends++; 
    [self.tableView reloadData]; 
} 

-(PersonCell *)parentCellForView:(id)theView 
{ 
    id viewSuperView = [theView superview]; 
    while (viewSuperView != nil) { 
     if ([viewSuperView isKindOfClass:[PersonCell class]]) { 
      return (PersonCell *)viewSuperView; 
     } 
     else { 
      viewSuperView = [viewSuperView superview]; 
     } 
    } 
    return nil; 
} 
相关问题