xcode 委托方法协议不起作用 - 目标 C
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11240937/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Delegate Method Protocol Not Working - Objective C
提问by kamalbhai
I am working on a splitView
application for my iPad
. I have implemented a UIButton
called as Upload
. On clicking on it, a UITableView
appears inside a UIPoverController
. On clicking on any of the contents, I want to display some respective site in my UIwebView
in UIDetailView
. For this I have implemented a delegate method protocol
. I have used the following lines of code in UploadTableViewController.h
file::
我正在splitView
为我的iPad
. 我已经实现了一个UIButton
叫做 as Upload
。单击它时, a 会UITableView
出现在 a 中UIPoverController
。单击任何内容时,我想在我UIwebView
的UIDetailView
. 为此,我实施了一个delegate method protocol
. 我在UploadTableViewController.h
文件中使用了以下代码行::
@protocol UploadTableViewDelegate <NSObject>
@required
- (void)selected:(NSString *)his;
@end
@interface UploadSpaceTableViewController : UITableViewController{
id<UploadSpaceTableViewDelegate> delegate;
}
@property (retain) id delegate;
@end
In the corresponding .m
file I have used the following lines of code ::
在相应的.m
文件中,我使用了以下代码行 ::
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (delegate != nil) {
NSString *hisSelected = [keys objectAtIndex:indexPath.row];
NSLog(@"%@ lolwa", hisSelected);
[delegate selected:hisSelected];
}
}
in the .m
file of class where I have implemented the function Selected
, the code is ::
在.m
我实现该函数的类文件中Selected
,代码是 ::
- (void)selected:(NSString *)Key {
NSLog(@"hello");
[self.UploadSpaceTableViewPopover dismissPopoverAnimated:YES];
}
-(IBAction)uploadpressed:(id)sender{
Upload.delegate = self;
self.Upload = [[UploadSpaceTableViewController alloc]
initWithStyle:UITableViewStylePlain];
self.UploadTableViewPopover = [[UIPopoverController alloc]
initWithContentViewController:UploadSpace];
[self.UploadTableViewPopover presentPopoverFromBarButtonItem:sender
permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
However, I am unable to get hello
(written in the function Selected
) NSLogged
in gdb. This is the first time that I am using this delegate method protocol
. I am unable to sort this out. Can someone help me out ? Thanks and regards.
但是,我无法在 gdb 中获取hello
(写在函数中Selected
)NSLogged
。这是我第一次使用这个delegate method protocol
。我无法解决这个问题。有人可以帮我吗 ?感谢致敬。
回答by CodaFi
[delegate keySelected:hisKeySelected];
is your first problem. You don't declare a delegate method named -keySelected:
, you declare a delegate method named -Selected:
.
[delegate keySelected:hisKeySelected];
是你的第一个问题。您没有声明名为 的委托方法-keySelected:
,而是声明了名为 的委托方法-Selected:
。
Your second problem is the fact that you are most definitely not the delegate of your table view. In order for a delegate method like -didSelectRowAtIndexPath:
to be called, you must be the table's delegate.
您的第二个问题是您绝对不是表视图的代表。为了-didSelectRowAtIndexPath:
调用类似的委托方法,您必须是表的委托。
PS, don't begin instances, or method names, with an uppercase letter. In ObjC, uppercase letters indicate a class.
PS,不要以大写字母开头实例或方法名称。在 ObjC 中,大写字母表示一个类。
EDIT: this is what your UploadSpaceTableViewController
header should look like:
编辑:这是你的UploadSpaceTableViewController
标题应该是什么样子:
@protocol UploadTableViewDelegate <NSObject>
@required
- (void)selected:(NSString *)his;
@end
@interface UploadSpaceTableViewController : UITableViewController<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, assign) id <UploadSpaceTableViewDelegate>delegate; //delegates are weak!!!
@end
And the .m, I will skip a lot of the unnecessary stuff:
而 .m,我会跳过很多不必要的东西:
-(void)viewDidLoad {
[self.tableView setDelegate:self];
[self.tableView setDataSource:self];
}
//other code
Furthermore, your delegate is declared retain, which is an absolutel No-No in ObjC. Declare is weak if using ARC, or assign if not.
此外,您的委托被声明为保留,这在 ObjC 中是绝对不可以的。如果使用 ARC,则声明是弱的,否则,如果不是,则分配。
You are also producing a nil delegate in in your -uploadPressed:
method by setting it before you explicitly own or initialize the object. Here's how it should look:
-uploadPressed:
通过在显式拥有或初始化对象之前设置它,您还在方法中生成了一个 nil 委托。这是它的外观:
self.Upload = [[UploadSpaceTableViewController alloc]initWithStyle:UITableViewStylePlain];
Upload.delegate = self;
回答by Pfitz
Delegation works like this
委托是这样工作的
- declare a protocol - you have done this
- declare a delegate property - you have done this
In the class which you want to be the delegate say it conforms to the protocoll
@interface MyClass : MySuperClass <UploadTableViewDelegate>
set the delegate property so the delegate class can get the delegate messages
uploadSpaceTVC.delegate = self;
call the delegate methods in your non delegate class (
UploadSpaceTableViewController
)[self.delegate selected:@"test"];
- 声明一个协议 - 你已经完成了这个
- 声明一个委托属性 - 你已经完成了
在你想成为委托的类中说它符合协议
@interface MyClass : MySuperClass <UploadTableViewDelegate>
设置委托属性,以便委托类可以获取委托消息
uploadSpaceTVC.delegate = self;
在非委托类 (
UploadSpaceTableViewController
) 中调用委托方法[self.delegate selected:@"test"];