财产“代表”在对象“AddEventViewController”上找不到

发布于 2025-01-01 14:06:08 字数 1173 浏览 1 评论 0原文

在开发人员库的帮助下,我尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

森林散布 2025-01-08 14:06:08

我明白苹果公司的例子可能会让你感到困惑。首先,下载 iPhoneCoreDataRecipes 的完整源代码(或至少在尝试理解这段代码时参考它)。

要真正了解这里发生的情况,您需要继续阅读“解除呈现的视图控制器”部分,然后点击链接“使用委派与其他控制器进行通信”。 (“代表团?”很奇怪......)

所以这就是发生的事情。所呈现的视图有一个“委托”,它是应该告诉“有趣”事情的对象。在这种情况下,“有趣”的事情是“嘿,我添加了一个食谱!”为了实现这一点,委托实现了一个协议,这仅仅意味着它承诺实现一些方法。在这种情况下,所需的方法是 recipeAddViewController:didAddRecipe:

AddViewController 有一个像这样的 delegate 属性:

@property(nonatomic, assign) id <RecipeAddDelegate> delegate;

这仅仅意味着委托必须符合指定的协议。委托本身表明它在其界面中这样做:

@interface RecipeListTableViewController : UITableViewController <RecipeAddDelegate, NSFetchedResultsControllerDelegate> {

请注意,由于 @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 a delegate property like this:

@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.

空宴 2025-01-08 14:06:08

在 Objective-C 中,您使用的任何属性都应该在某处声明。在父类(在您的情况下为 UIViewController)中声明的任何属性都由所有派生类(在您的情况下为AddEventViewController)继承。

AddEventViewController 继承了 UIViewController,但是 delegate 属性既没有在第一个中声明,也没有在第二个中声明。这就是编译器不高兴的原因。

你应该声明它。如下所示:

@interface AddEventViewController : UIViewController

@property (nonatomic, assign) id delegate;  

@end

@implementation AddEventViewController

@synthesize delegate;

@end

委托通常使用 assign 属性声明,以防止循环保留(例如 A 保留 BB > 保留 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 inherits UIViewController, but the delegate 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:

@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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文