2014-09-27 207 views
0

我已经看了一些教程,甚至观看了苹果的WWDC 2011视频,了解如何创建自定义协议向后发送数据,看起来我错过了一些东西,但我无法准确地确定是什么。我有一个addUrination视图控制器,它从视图控制器图表上被压入堆栈。我在addUrination.h中创建了一个协议和一个委托属性,以便在提交数据条目时能够向图表发送消息。提交数据条目时,我调用我在自定义协议中创建的消息,并已在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]; 
      [email protected]""; 
      [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

看来一切正常,你只需要检查方法主叫线路是否正在执行或不检查就进入这些线或不和自我的参考。请让我知道

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

没错,就是这样。 Charts.m是页面控制器的页面,所以segue实际上是从包含页面视图控制器的视图控制器容器调用的。现在我无法将一个项目添加到导航栏来从charts.m执行segue.m – 2014-09-27 19:44:10

+0

我试图通过调用self.parentViewController.navigationItem.rightBarButton将该按钮添加到导航栏中,但那不起作用。将消息发送到页面而不是容器中的信息更有用 – 2014-09-27 19:47:02

+0

请提供更多详细信息?或代码捕捉。 – Sandy 2014-09-29 15:19:11