委派和模态视图控制器

时间:2010-10-12 18:03:33

标签: objective-c delegates ios4 modalviewcontroller delegation

根据View Controller编程指南,委派是解除模态视图的首选方法。

按照Apple自己的Recipe示例,我已实现以下内容,但不断收到警告: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];
    }



我认为所有必需的元素都存在于正确的位置?

由于

1 个答案:

答案 0 :(得分:4)

您尚未指定delegate的{​​{1}}属性必须符合AddName协议。

AddName.h:

中使用此代码
NameDelegate