财产“代表”在对象“AddEventViewController”上找不到
在开发人员库的帮助下,我尝试使用 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];
错误
在线显示: addController.delegate = self;
此代码是直接从库复制的。我正在使用 Xcode 4.2 和 Storyboard(如果这可能有帮助的话)。
更新: 这是 AddEventViewController.h:
#import <UIKit/UIKit.h>
@interface AddEventViewController : UIViewController
@end
你会告诉我我错误地创建了这个 ViewController 吗?如果你这么友善,请解释一下为什么不说“如何”?
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.
- (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;
This code is copied straight from the Library. I am using Xcode 4.2 and a Storyboard if that might help.
UPDATE:
This is 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我明白苹果公司的例子可能会让你感到困惑。首先,下载 iPhoneCoreDataRecipes 的完整源代码(或至少在尝试理解这段代码时参考它)。
要真正了解这里发生的情况,您需要继续阅读“解除呈现的视图控制器”部分,然后点击链接“使用委派与其他控制器进行通信”。 (“代表团?”很奇怪......)
所以这就是发生的事情。所呈现的视图有一个“委托”,它是应该告诉“有趣”事情的对象。在这种情况下,“有趣”的事情是“嘿,我添加了一个食谱!”为了实现这一点,委托实现了一个协议,这仅仅意味着它承诺实现一些方法。在这种情况下,所需的方法是
recipeAddViewController:didAddRecipe:
。AddViewController
有一个像这样的delegate
属性:这仅仅意味着委托必须符合指定的协议。委托本身表明它在其界面中这样做:
请注意,由于 @Yuras 解释的原因,这被标记为“分配”。但如果您正在编写针对 iOS 5 的新代码,则应该使用
weak
而不是assign
。如果其引用的对象被释放,weak
属性将自动设置为nil
。这样更安全。没有悬空指针。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).
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:
.AddViewController
has adelegate
property like this:That just means that the delegate must conform to the named protocol. The delegate itself indicates that it does so in its interface:
Note that this is marked
assign
for reasons @Yuras explains. But if you're writing new code targeted at iOS 5, you should useweak
instead ofassign
.weak
properties are automatically set tonil
if their referenced object is deallocated. It's just safer that way. No dangling pointers.在 Objective-C 中,您使用的任何属性都应该在某处声明。在父类(在您的情况下为
UIViewController
)中声明的任何属性都由所有派生类(在您的情况下为AddEventViewController
)继承。AddEventViewController
继承了UIViewController
,但是delegate
属性既没有在第一个中声明,也没有在第二个中声明。这就是编译器不高兴的原因。你应该声明它。如下所示:
委托通常使用
assign
属性声明,以防止循环保留(例如A
保留B
和B
> 保留A
)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).AddEventViewController
inheritsUIViewController
, but thedelegate
property is not declared neither in the first, nor in the second. That is why compiler is not happy.You should declare it. Something like the next:
Delegates are usually declared with
assign
attribute to prevent circle retains (e.g.A
retainsB
andB
retainsA
)