xcode 在对象“AddEventViewController”上找不到属性“delegate”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9071708/
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
Property 'delegate' not found on object 'AddEventViewController'
提问by Solid I
With the help of the Developer Library, I am trying to work with the EventKit and EventKitUI Frameworks. I've hit a very early roadblock. I have copied and pasted code from the library found here. I have added a view controller called 'AddEventViewController' a button to the Navigation Bar in my ViewController and I am using this code to invoke it.
在开发人员库的帮助下,我正在尝试使用 EventKit 和 EventKitUI 框架。我很早就遇到了障碍。我已经从这里找到的库中复制并粘贴了代码。我在 ViewController 的导航栏中添加了一个名为“AddEventViewController”的视图控制器,我正在使用此代码来调用它。
- (IBAction)add:(id)sender {
AddEventViewController *addController = [[AddEventViewController alloc]
init];
addController.delegate = self;
UINavigationController *navigationController = [[UINavigationController alloc]
initWithRootViewController:addController];
[self presentViewController:navigationController animated:YES completion: nil];
}
}
The error shows on line: addController.delegate = self;
在线显示错误: addController.delegate = self;
This code is copied straight from the Library. I am using Xcode 4.2 and a Storyboard if that might help.
此代码直接从库中复制。如果可能有帮助,我正在使用 Xcode 4.2 和 Storyboard。
UPDATE: This is AddEventViewController.h:
更新:这是 AddEventViewController.h:
#import <UIKit/UIKit.h>
@interface AddEventViewController : UIViewController
@end
You're going to tell me I created this ViewController incorrectly? Please explain why just not "how" if you'd be so nice?
你会告诉我我错误地创建了这个 ViewController 吗?请解释一下,如果你这么好,为什么不“如何”?
回答by Rob Napier
I see how Apple's example here has likely confused you here. First, download the full source for iPhoneCoreDataRecipes(or at least reference it while trying to understand this code).
我看到这里 Apple 的例子很可能让你感到困惑。首先,下载iPhoneCoreDataRecipes的完整源代码(或至少在尝试理解此代码时参考它)。
To really understand what's going on here, you need to read on down to the section called "Dismissing a Presented View Controller" and then follow the link to "Use a Delegation to Communicate With Other Controllers." ("a Delegation?" Very strange....)
要真正理解这里发生的事情,您需要继续阅读名为“解除呈现的视图控制器”的部分,然后点击“使用委派与其他控制器进行通信”的链接。(“一个代表团?”很奇怪……)
So here's what's going on. The presented view has a "delegate" which is the object it's supposed to tell "interesting" things to. In this case, "interesting" things are "hey, I added a recipe!" To achieve this, the delegate implements a protocol, which just means it promises to implement some methods. In this case, the required method is recipeAddViewController:didAddRecipe:
.
这就是正在发生的事情。呈现的视图有一个“委托”,它是它应该告诉“有趣”事物的对象。在这种情况下,“有趣”的事情是“嘿,我添加了一个食谱!” 为了实现这一点,委托实现了一个协议,这意味着它承诺实现一些方法。在这种情况下,所需的方法是recipeAddViewController:didAddRecipe:
。
AddViewController
has a delegate
property like this:
AddViewController
有这样的delegate
属性:
@property(nonatomic, assign) id <RecipeAddDelegate> delegate;
That just means that the delegate must conform to the named protocol. The delegate itself indicates that it does so in its interface:
那只是意味着委托必须符合命名协议。委托本身表明它在其界面中这样做:
@interface RecipeListTableViewController : UITableViewController <RecipeAddDelegate, NSFetchedResultsControllerDelegate> {
Note that this is marked assign
for reasons @Yuras explains. But if you're writing new code targeted at iOS 5, you should use weak
instead of assign
. weak
properties are automatically set to nil
if their referenced object is deallocated. It's just safer that way. No dangling pointers.
请注意,这是assign
出于@Yuras 解释的原因标记的。但是,如果你在iOS 5的编写有针对性的新的代码,你应该使用weak
代替assign
。如果它们的引用对象被释放weak
,nil
则属性会自动设置为。那样只会更安全。没有悬空的指针。
回答by Yuras
In Objective-C any property you are using should be declared somewhere. Any properties, that are declared in parent class (UIViewController
in your case) are inherited by all the derived classes (AddEventViewController
in your case).
在 Objective-C 中,您使用的任何属性都应该在某处声明。在父类(UIViewController
在您的情况下)中声明的任何属性都由所有派生类(AddEventViewController
在您的情况下)继承。
AddEventViewController
inherits UIViewController
, but the delegate
property is not declared neither in the first, nor in the second. That is why compiler is not happy.
AddEventViewController
inherits UIViewController
,但该delegate
属性既未在第一个中声明,也未在第二个中声明。这就是编译器不高兴的原因。
You should declare it. Something like the next:
你应该申报。类似下一个:
@interface AddEventViewController : UIViewController
@property (nonatomic, assign) id delegate;
@end
@implementation AddEventViewController
@synthesize delegate;
@end
Delegates are usually declared with assign
attribute to prevent circle retains (e.g. A
retains B
and B
retains A
)
委托通常用assign
属性声明以防止循环保留(例如A
保留B
和B
保留A
)