2014-09-27 204 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