[英]UITableViewController as ChildViewController not calling delegate methods
[英]Custom delegate not calling UITableViewController method
我有一個自定義的UIButton類,如下所示:
。H
#import <UIKit/UIKit.h>
@class FriendButton;
@protocol LongPressedButtonDelegate
- (void)buttonIsLongPressed:(FriendButton *)button;
@end
@interface FriendButton : UIButton
@property (nonatomic, weak) id<LongPressedButtonDelegate > delegate;
@end
.m
@implementation FriendButton
//this is called from the interface builder
-(id) initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:(NSCoder *)aDecoder];
NSLog(@"init called");
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPress:)];
[self addGestureRecognizer:longPress];
return self;
}
-(void)longPress:(id)sender {
NSLog(@"long press");
[self.delegate buttonIsLongPressed:self];
}
@end
我的按鈕是在界面構建器中設置的,它們包含在UITableView單元格中。 在我的UITableViewController中,我有:
-(void)buttonIsLongPressed:(FriendButton *)button {
NSLog(@"Delegate method!");
}
並且它的控制器符合協議。 長按手勢有效,但是未調用委托方法。 我不確定為什么它不起作用。 是因為我必須將每個按鈕委托設置為UITableViewController嗎? 如果是這樣,我該怎么做? 這些按鈕在界面構建器中設置。
以此方式定義您的UIButton屬性
@interface FriendButton : UIButton
@property (nonatomic, weak) IBOutlet id<LongPressedButtonDelegate > delegate;
@end
然后轉到Interface Builder,然后右鍵單擊UIButton,您將看到將該委托鏈接到UITableViewController的委托。 它應該工作
在UITableViewController的cellForRowAtIndexPath
實現中,您將需要執行以下操作:
cell.button.delegate = self;
編輯:這是如果您要以編程方式進行。 如果要在情節提要中進行操作,請參考IBOutlets周圍的答案。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.