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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 00:43:06  来源:igfitidea点击:

Delegate Method Protocol Not Working - Objective C

iphoneobjective-ciosxcodeipad

提问by kamalbhai

I am working on a splitViewapplication for my iPad. I have implemented a UIButtoncalled as Upload. On clicking on it, a UITableViewappears inside a UIPoverController. On clicking on any of the contents, I want to display some respective site in my UIwebViewin UIDetailView. For this I have implemented a delegate method protocol. I have used the following lines of code in UploadTableViewController.hfile::

我正在splitView为我的iPad. 我已经实现了一个UIButton叫做 as Upload。单击它时, a 会UITableView出现在 a 中UIPoverController。单击任何内容时,我想在我UIwebViewUIDetailView. 为此,我实施了一个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 .mfile 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 .mfile 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) NSLoggedin 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(写在函数中SelectedNSLogged。这是我第一次使用这个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 UploadSpaceTableViewControllerheader 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"];