无法获得自定义@protocol在iOS上工作 [英] Can't get custom @protocol working on iOS

查看:148
本文介绍了无法获得自定义@protocol在iOS上工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

注意:以下是使用启用了自动引用计数(ARC)的iOS。我认为ARC可能有很多与为什么它不工作,因为这是根据我通过谷歌找到的例子设置。



我试图创建一个协议,通知用户从UITableView中选择的文件名。



FileListViewController.h

  @protocol FileListDelegate< NSObject> 
- (void)didSelectFileName:(NSString *)fileName;

@end

@interface FileListViewController:UITableViewController
{
@private
NSArray * fileList;
id< FileListDelegate>代表;
}
@property(nonatomic,retain)NSArray * fileList;
@property(nonatomic,assign)id< FileListDelegate>代表;

@end

FileListViewController.m

  #importFileListViewController.h

@implementation FileListViewController

@synthesize fileList;
@synthesize delegate;

这会导致

错误。

  @synthesize delegate; 

这是FileListViewController.m:错误:自动引用计数问题:现有的ivar'委托' unsafe_unretained属性'delegate'必须是__unsafe_unretained



如果我改变FileListViewController.h把__weak和(weak),那么它会运行。

  @protocol FileListDelegate< NSObject> 
- (void)didSelectFileName:(NSString *)fileName;

@end

@interface FileListViewController:UITableViewController
{
@private
NSArray * fileList;
__weak id< FileListDelegate>代表;
}
@property(nonatomic,retain)NSArray * fileList;
@property(weak)id< FileListDelegate>代表;

@end

但是当我尝试设置代理时,应用崩溃。一个称为ImportViewController的视图是从'FileListViewController'创建一个视图,并将委托设置为它自己(ImportViewController),所以我可以实现我的自定义协议'didSelectFileName'。我得到的错误是



* 由于未捕获异常NSInvalidArgumentException终止应用程序,原因:' - [ImportViewController setDelegate:]:unrecognized选择器发送到实例0x6c7d430'



我运行的代码是



ImportViewController.m

   - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{
FileListViewController * fileListViewController = [self.storyboard instantiateViewControllerWithIdentifier:@filelist];

[fileListViewController setDelegate:self];
[self.navigationController pushViewController:fileListViewController animated:YES];

}

我的问题是:




  • 为什么放置(weak)和__weak使它工作?我不
    明白为什么这个工作,因为我发现它googling和没有一个
    的解释。

  • 为什么无法使用
    '[fileListViewController setDelegate:self];'设置我的委托?似乎
    编译器不知道委托存在。


解决方案

看起来像:

  FileListViewController * fileListViewController = 
[self.storyboard instantiateViewControllerWithIdentifier:@filelist];

您没有获得 FileListViewController 对象。看看它说的消息:



- [ImportViewController setDelegate:]:无法识别的选择器发送到实例0x6c7d430 p>

这就是为什么您的应用程式崩溃。还要尝试定义一个retain属性,而不是仅仅分配,如果代理被释放在其他地方,您的应用程序不会崩溃。


Note: the below is using iOS with Automatic Reference Counting (ARC) enabled. I think ARC may have a lot to do with why it isn't working as this is set up as per examples i've found via google.

I am trying to create a protocol to notify a delegate of the filename the user selects from a UITableView.

FileListViewController.h

@protocol FileListDelegate <NSObject>
- (void)didSelectFileName:(NSString *)fileName;

@end

@interface FileListViewController : UITableViewController
{
    @private
        NSArray *fileList;
        id <FileListDelegate> delegate;
}
@property (nonatomic, retain) NSArray *fileList;
@property (nonatomic, assign) id <FileListDelegate> delegate;

@end

FileListViewController.m

#import "FileListViewController.h"

@implementation FileListViewController

@synthesize fileList;
@synthesize delegate;

This gives an error at the

@synthesize delegate;

line which is "FileListViewController.m: error: Automatic Reference Counting Issue: Existing ivar 'delegate' for unsafe_unretained property 'delegate' must be __unsafe_unretained"

If i change FileListViewController.h putting __weak and (weak) then it will run.

@protocol FileListDelegate <NSObject>
- (void)didSelectFileName:(NSString *)fileName;

@end

@interface FileListViewController : UITableViewController
{
    @private
        NSArray *fileList;
        __weak id <FileListDelegate> delegate;
}
@property (nonatomic, retain) NSArray *fileList;
@property (weak) id <FileListDelegate> delegate;

@end

But when I try to set the delegate the app crashes. A view called 'ImportViewController' is creating a view from 'FileListViewController' and setting the delegate to itself (ImportViewController) so I can implement my custom protocol of 'didSelectFileName'. The error I get is;

* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[ImportViewController setDelegate:]: unrecognized selector sent to instance 0x6c7d430'

The code I am running is;

ImportViewController.m

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    FileListViewController *fileListViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"filelist"];

    [fileListViewController setDelegate:self];
    [self.navigationController pushViewController:fileListViewController animated:YES];

}

My Questions are:

  • Why does putting (weak) and __weak in make it work? I don't understand why this works as I found it googling and there wasn't an explanation.
  • Why can't I set my delegate using this '[fileListViewController setDelegate:self];' ? It seems like the compiler doesn't know 'delegate' exists.

解决方案

It seems that with :

FileListViewController *fileListViewController =
    [self.storyboard instantiateViewControllerWithIdentifier:@"filelist"];

you didn't get an FileListViewController object. Look at the message it says :

-[ImportViewController setDelegate:]: unrecognized selector sent to instance 0x6c7d430

and that why your app crashes. Also try to define a retain property, instead of just assign, in case the delegate is deallocated elsewhere, your app won't crash.

这篇关于无法获得自定义@protocol在iOS上工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆