0

出于某种原因,我的自定义委托为零。这是代码:

。H

@protocol AssignmentDelgate <NSObject>

-(void)newAssignment:(AssignmentInfo *)assignment;

@end
@property (nonatomic,weak)id<AssignmentDelgate> otherdelegate;

.m

- (IBAction)addTheInfo:(id)sender {
    [self.otherdelegate newAssignment:self.assignmentInfo];
    NSLog(@"%@",self.otherdelegate); //Returning nil!
}

另一个VC.h:

@interface AssignmentListViewController : UITableViewController<AssignmentDelgate,UITextFieldDelegate>

@property(strong,nonatomic) AddEditViewController *vc;

@property (strong, nonatomic) NSMutableArray *alist;

另一个VC.m

-(void)newAssignment:(AssignmentInfo *)assignment
{
    [self.alist addObject:assignment];
}
- (void)viewDidLoad
{
    [super viewDidLoad];

    self.vc.otherdelegate = self;
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

为什么代表为零?我重写了应用程序,但没有任何区别。项目链接: http ://steveedwin.com/AssignmentAppTwo.zip

4

1 回答 1

1

好的,您正在使用 segue 推送。

您需要将您的 prepareForSegue 更改为:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{

    if ([segue.identifier isEqualToString:@"addAssignment"])
    {
        AddEditViewController *addEditController = segue.destinationViewController;
        [addEditController setOtherdelegate:self];
    }
}

无需实例化 self.vc,因为故事板正在为您执行此操作。

解释 由于您使用的是情节提要,情节提要实际上是在实例化您的视图控制器。因此,您已经从按钮创建了一个链接,以通过 segue 打开您的下一个控制器。

当您点击按钮时,它会调用 UIViewController 的 performSegueWithIdentifier:该方法会为您创建您的 destinationViewController,您可以在 prepareForSegue 中拦截它。

那么在您的应用程序中发生了什么,您在 viewDidLoad 期间创建了 AddEditViewController 并将其保存在内存中,当您点击按钮调出 AddEditViewController 时,您实际上是通过 segues 创建了该类的一个新实例。

于 2013-10-21T00:17:38.183 回答