委托不会发回对象

时间:2014-08-07 03:07:47

标签: ios objective-c delegates

我正在尝试将对象发送回视图控制器,但这似乎并没有触发。我没有使用segues,我已经在这里填写了之前的答案,但他们似乎要么处理segues,要么与IOS 7无关。

我真的很感激任何帮助。

Main controller.h

#import "Calculation.h"
@class CalculationDetailViewController;

@protocol CalculationDetailViewControllerDelegate <NSObject>

-(void)CalculationDetailViewDidCancel:(CalculationDetailViewController *)controller;
-(void)CalculationDetailView:(CalculationDetailViewController *)controller didFinishAddingItem:(Calculation *)item;

@end


#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#import "Soil.h"
#import "Vehicle.h"

#import "SoilCreatorViewController.h"
#import "VehicleCreatorViewController.h"

@class Calculation;



@interface CalculationDetailViewController : UIViewController <UIPickerViewDataSource,UIPickerViewDelegate,CLLocationManagerDelegate,VehicleCreatorViewDelegate,SendSoilBack>{

    UIPickerView *vehiclePicker;
    NSMutableArray *vehicleArray;
    UIPickerView *soilPicker;
    NSMutableArray *soilArray;
    IBOutlet UIScrollView *scroller;
    Soil *soil;

}...

主控制器.m的相关功能

- (IBAction)LaunchSoilView:(id)sender {

    SoilCreatorViewController *newSoilView = [[SoilCreatorViewController alloc]initWithNibName:@"SoilCreatorView" bundle:NULL];
    [self.navigationController pushViewController:newSoilView animated:YES];
}

- (void)SendSoilToCalcController:(Soil *)asoil{
    NSLog(@"received soil");
    [self.soilArray addObject:asoil];
    [self.soilPicker reloadAllComponents];
}

子视图的controller.h

#import "Soil.h"
#import <UIKit/UIKit.h>

@class SoilCreatorViewController;

@protocol SendSoilBack<NSObject>
-(void)SendSoilToCalcController:(Soil*) soil;
@property (nonatomic,weak) id<SendSoilBack> delegate;
@end



@interface SoilCreatorViewController : UIViewController

@property (weak,nonatomic) id<SendSoilBack> delegate;

子控制器的.m相关功能。在此之前合成代表。

- (IBAction)saveButton:(id)sender {

    self.thisSoil = [[Soil alloc] init];

    _thisSoil.soilType = self.soilNameField.text;
    NSLog(@"soil  type field %@", self.soilNameField.text);
    NSLog(@"soil type: %@", _thisSoil.soilType);

    _thisSoil.frictionAngle = [self.frictionAngleValue.text integerValue];

    if ([self.soilUnitsSwitch isOn] ) {
        _thisSoil.cohesion = [self.cohesionValue.text doubleValue];
        _thisSoil.unitWeight = [self.unitWeightValue.text doubleValue];
    }else{
        _thisSoil.cohesion = [self.cohesionValue.text doubleValue];
        _thisSoil.unitWeight = [self.unitWeightValue.text doubleValue];
    }
    [delegate SendSoilToCalcController:_thisSoil];
    [self.navigationController popViewControllerAnimated:YES];
}

2 个答案:

答案 0 :(得分:2)

你忘了设置代理。

- (IBAction)LaunchSoilView:(id)sender {
  SoilCreatorViewController *newSoilView = [[SoilCreatorViewController alloc]initWithNibName:@"SoilCreatorView" bundle:NULL];
  newSoilView.delegate = self; // You need to set delegate here.
  [self.navigationController pushViewController:newSoilView animated:YES];
}

答案 1 :(得分:0)

不需要在协议中保留财产

@protocol SendSoilBack<NSObject>
-(void)SendSoilToCalcController:(Soil*) soil; 
//@property (nonatomic,weak) id<SendSoilBack> delegate; //Not needed here 
@end

和maincontorller

- (IBAction)LaunchSoilView:(id)sender {

    SoilCreatorViewController *newSoilView = [[SoilCreatorViewController alloc]initWithNibName:@"SoilCreatorView" bundle:NULL];
[newSoilView setDelegate:self] // add this line
    [self.navigationController pushViewController:newSoilView animated:YES];
}

然后你会被触发