委派和模式视图控制器

发布于 2024-09-27 07:35:35 字数 1434 浏览 6 评论 0原文

根据视图控制器编程指南,委托是关闭模态视图的首选方法。

按照苹果自己的食谱示例,我实现了以下内容,但不断收到警告,指出找不到 addNameController:didAddName 方法...

NameDelegate.h
    @protocol NameDelegate
    - (void)addNameController:(AddName *)addNameController didAddName:(NSString *)name;
    @end

AddName.h
    @interface AddName : UIViewController {
        UITextField *nameField;
        id delegate;
    }
    - (IBAction)doneAction;
    - (id)delegate;
    - (void)setDelegate:(id)newDelegate;
    @property (nonatomic, retain) IBOutlet UITextField *nameField;
    @end

AddName.m
    - (IBAction)doneAction {
        [delegate addNameController:self didAddName:[nameField text]];
    }

    - (id)delegate {
        return delegate;
    }

    - (void)setDelegate:(id)newDelegate {
        delegate = newDelegate;
    }

ItemViewController.h
    #import "NameDelegate.h"
    @interface ItemViewController : UITableViewController <NameDelegate>{
    }
    @end

ItemViewController.m
    - (void)addItem:(id)sender {

        AddName *addName = [[AddName alloc] init];
        addName.delegate = self;
        [self presentModalViewController:addName animated:YES];
    }

    - (void)addNameController:(AddName *)addNameController didAddName:(NSString *)name {
        //Do other checks before dismiss... 
        [self dismissModalViewControllerAnimated:YES];
    }



我认为所有必需的元素都在那里并且在正确的位置?

谢谢

According to the View Controller Programming Guide, delegation is the preferred method to dismiss a modal view.

Following Apple's own Recipe example, i have implemented the following, but keep getting warnings that the addNameController:didAddName method is not found...

NameDelegate.h
    @protocol NameDelegate
    - (void)addNameController:(AddName *)addNameController didAddName:(NSString *)name;
    @end

AddName.h
    @interface AddName : UIViewController {
        UITextField *nameField;
        id delegate;
    }
    - (IBAction)doneAction;
    - (id)delegate;
    - (void)setDelegate:(id)newDelegate;
    @property (nonatomic, retain) IBOutlet UITextField *nameField;
    @end

AddName.m
    - (IBAction)doneAction {
        [delegate addNameController:self didAddName:[nameField text]];
    }

    - (id)delegate {
        return delegate;
    }

    - (void)setDelegate:(id)newDelegate {
        delegate = newDelegate;
    }

ItemViewController.h
    #import "NameDelegate.h"
    @interface ItemViewController : UITableViewController <NameDelegate>{
    }
    @end

ItemViewController.m
    - (void)addItem:(id)sender {

        AddName *addName = [[AddName alloc] init];
        addName.delegate = self;
        [self presentModalViewController:addName animated:YES];
    }

    - (void)addNameController:(AddName *)addNameController didAddName:(NSString *)name {
        //Do other checks before dismiss... 
        [self dismissModalViewControllerAnimated:YES];
    }

I think all the required elements are there and in the right place?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

你列表最软的妹 2024-10-04 07:35:35

您尚未指定 AddNamedelegate 属性必须符合 NameDelegate 协议。

AddName.h:中使用此代码

#import "NameDelegate.h"

@interface AddName : UIViewController {
    UITextField *nameField;
    id <NameDelegate> delegate;
}

@property(nonatomic, retain) IBOutlet UITextField *nameField;
@property(nonatomic, assign) id <NameDelegate> delegate;

- (IBAction)doneAction;

@end

You haven't specified that the delegate property of AddName has to conform to the NameDelegate protocol.

Use this code in AddName.h:

#import "NameDelegate.h"

@interface AddName : UIViewController {
    UITextField *nameField;
    id <NameDelegate> delegate;
}

@property(nonatomic, retain) IBOutlet UITextField *nameField;
@property(nonatomic, assign) id <NameDelegate> delegate;

- (IBAction)doneAction;

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