自定义协议不发送消息

时间:2014-09-27 18:59:28

标签: ios protocols

我查看了一些教程,甚至观看了Apple的WWDC 2011视频,了解如何创建自定义协议以向后发送数据,似乎我错过了一些东西,但我不能确切地指出了什么。我有一个addUrination视图控制器,它从视图控制器图表推入堆栈。我在addUrination.h中创建了一个协议和一个委托属性,以便在提交数据条目时能够向Charts发送消息。提交数据条目时,我调用我在自定义协议中创建的消息并在Charts.m中实现,但由于我的NSLog调用从未出现在控制台中,因此永远不会发送消息。我记得在准备来自Charts.m的segue时设置代表。感谢您提供任何信息,因为了解其中的错误将极大地帮助您学习如何向后发送邮件。

AddUrination.h

#import <UIKit/UIKit.h>

@class AddUrination;
@protocol AddUrinationDelegate <NSObject>

- (void)addUrinationViewController:(AddUrination *)controller didFinishEnteringUrination:(NSNumber *)urination;

@end

@interface AddUrination : UITableViewController<UITextFieldDelegate>

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

@end

AddUrination.m

-(IBAction)addUrination:(id)sender
{
PFObject *amount=[PFObject objectWithClassName:@"urinationAmount"];
NSNumberFormatter *number=[[NSNumberFormatter alloc]init];
[number setNumberStyle:NSNumberFormatterDecimalStyle];
NSNumber *urinateAmount=[number numberFromString:self.addUrinationTextField.text];

/*Create activity indicator*/
UIActivityIndicatorView *spinner=[[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
[spinner setCenter:CGPointMake(self.tableView.frame.size.width/2.0,(self.tableView.frame.size.height-self.keyboardHeight)/2.0)];
spinner.layer.backgroundColor=[[UIColor blackColor]CGColor];
spinner.layer.cornerRadius=10;
[self.view addSubview:spinner];

[spinner startAnimating];
if(urinateAmount!=nil){
    [amount setObject:urinateAmount forKey:@"amountOfUrine"];
    PFUser *user=[PFUser currentUser];
    [amount setObject:user forKey:@"user"];
    [amount saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if(!error) {
            [spinner stopAnimating];
            UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Thank You!" message:@"Your urination amount has been successfully saved" delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles:nil, nil];
            [alert show];
            UrinationData *dataSettings=[UrinationData sharedUrinationData];
            [dataSettings setUrinationDataChanged:YES];
            self.addUrinationTextField.text=@"";
            [self.delegate addUrinationViewController:self didFinishEnteringUrination:urinateAmount];
            [self.navigationController popViewControllerAnimated:YES];
        }
        else{
            [spinner stopAnimating];
            UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Uh-oh!" message:@"There was an error on our end. Please try again!" delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles:nil, nil];
            [alert show];
        }
    }];
}
else{
    [spinner stopAnimating];
    UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Uh-oh!" message:@"Please enter a valid amount" delegate:nil cancelButtonTitle:@"Sorry" otherButtonTitles:nil, nil];
    [alert show];
}
}

Charts.h

#import <UIKit/UIKit.h>
#import "CorePlot-CocoaTouch.h"
#import "CPTAnimation.h"
#import "AddUrination.h"


@interface Charts :     UIViewController<CPTPlotDataSource,CPTPlotSpaceDelegate,CPTScatterPlotDelegate,CPTScatterPlotDataSource,CPTAnimationDelegate,UIGestureRecognizerDelegate,UINavigationControllerDelegate,UIAlertViewDelegate,AddUrinationDelegate>

@property(nonatomic,strong)CPTXYPlotSpace *plotSpace;
@property(nonatomic,strong)CPTXYGraph *graph;
@property(nonatomic,strong)CPTGraphHostingView *hostingView;
@property(nonatomic,strong)NSString *graphTitle;
@property(nonatomic,strong)UISegmentedControl *chartsSegmentedControl;
@property(nonatomic,strong)CPTPlotSpaceAnnotation *urinationAnnotation;

@end

Charts.m

-(void)addUrinationViewController:(AddUrination *)controller didFinishEnteringUrination:(NSNumber *)urination{
NSLog(@"Urination was entered from add urination screen");
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
if([segue.identifier isEqualToString:@"AddUrinationSegue"]){
    AddUrination *addUrination=[segue destinationViewController];
    addUrination.delegate=self;
}
}

1 个答案:

答案 0 :(得分:1)

似乎一切都很好,你只需要检查方法调用行是否正在执行,检查控制是否在这些行中进行和自我参考。请让我知道

if([segue.identifier isEqualToString:@"AddUrinationSegue"]){
    AddUrination *addUrination=[segue destinationViewController];
    addUrination.delegate=self;
}