ios - 自定义协议(protocol)不发送消息

标签 ios protocols

我看过一些教程,甚至还观看了 Apple 的 WWDC 2011 视频,内容是关于如何创建自定义协议(protocol)以向后发送数据,我似乎遗漏了一些东西,但我无法准确指出是什么。我有一个 addUrination View Controller ,它从 View Controller 图表中被插入堆栈。我在 addUrination.h 中创建了一个协议(protocol)和一个委托(delegate)属性,以便在提交数据条目时能够向图表发送消息。提交数据条目后,我调用在自定义协议(protocol)中创建并已在 Charts.m 中实现的消息,但该消息从未发送过,因为我的 NSLog 调用从未出现在控制台中。我还记得在准备从 Charts.m 进行转接时设置代表。感谢您提供任何信息,因为了解其中的问题将极大地帮助学习如何向后发送消息。

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;
}
}

最佳答案

似乎一切正常,您只需要检查方法调用行是否正在执行并检查控制是否在这些行中以及 self 的引用。请告诉我

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

关于ios - 自定义协议(protocol)不发送消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26078084/

相关文章:

ios - 在 Swift 中遵守自制协议(protocol)时出错

objective-c - 从具有 objective-c 类别的类扩展协议(protocol)

objective-c - 为什么您希望类私下遵守协议(protocol)?

ios - 找不到支持键盘 type 4 的键盘 iPhone-PortraitChoco-NumberPad

javascript - Safari window.matchMedia 处理程序未调用

ios - 有人能告诉我导航器窗口中的这些问号是什么意思吗? (Xcode 4.2)

operating-system - 我如何了解专有硬件通信?

c++ - 自己的 C++ 网络协议(protocol) : How to define unique binary telegram values

ios - 在 App Store 上的应用程序中使用 itms-services 链接

iphone - 如何从 plist 中获取乌尔都语单词?