由于某种原因,我的自定义委托为nil。这是代码:
。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
最佳答案
好的,您正在使用segue push。
您需要将您的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创建类的新实例。
关于ios - 自定义代表为零,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19484431/