【发布时间】:2014-07-22 21:38:21
【问题描述】:
我有三门课。 AlertDetailsVC、UtilizationDetailsVC 和 SelectIOVC。
UtilizationDetailsVC 本质上是AlertDetailsVC 的一个副本,只是做了一些小的修改。
当用户为我的AlertDetailsVC 打开Scene 时,他可以按下Table 中名为Applies To 的按钮。当他按下这个Applies To按钮时,程序然后Segues到一个新的Scene,它使用SelectIOVC;然后它将该场景的Delegate 设置为等于AlertDetailsVC 中定义的Delegate。
然后当用户在Navigation Bar上点击DoneSelectIOIOVCScene;然后SelectIOVC 类将用户选择的一些数据和信息返回给AlertDetailsVC。
此流程类似地复制到UtilizationDetailsVC。
我在应该被调用的方法中添加了几行 NSLog(@"%s",__PRETTY_FUNCTION__);。
现在;当我在我的AlertDetailsVC 中点击Done 时,方法被正确调用,程序Segues 从SelectIOVC 返回到我的AlertDetailsVC 并更新用户输入的该页面上的信息。
但是我的问题是;当我在UtilizationDetailsVC 页面上做同样的事情时;并按下它的Done 按钮;程序不Segue返回。
我不确定;我的日志显示在AlertDetailsVC 的情况下调用了第一个SelectIOVCDidSave;但它从未被UtilizationDetailsVC 调用过。
相关代码:
利用:
`UtilizationDetailsVC.h`:
@class UtilizationDetailVC;
@protocol UtilizationDetailsVCDelegate <NSObject>
- (void)utilizationDetailsVCDidCancel: (UtilizationDetailVC *)controller;
- (void)utilizationDetailsVCDidDelete: (UtilizationDetailVC *)controller;
- (void)utilizationDetailsVCDidSave: (UtilizationDetailVC *)controller;
@end
@interface UtilizationDetailVC : UITableViewController <UITextFieldDelegate, UIActionSheetDelegate, SelectTimeVCDelegate, SelectDaysVCDelegate, SelectIOVCDelegate, SelectIODetailsVCDelegate, SelectTimeLengthVCDelegate, MessageVCDelegate, UtilizationContactsVCDelegate, SelectDateVCDelegate>
@property (weak, nonatomic) IBOutlet UIBarButtonItem *doneButton;
@property (strong, nonatomic) NCUtilization *utilization;
@property (weak, nonatomic) id <UtilizationDetailsVCDelegate> delegate;
@property (strong, nonatomic) NSString *identifier;
@property (strong, nonatomic) NSIndexPath *indexPath;
@property (strong, nonatomic) IBOutlet UITableViewCell *cellEnabled;
@property (strong, nonatomic) IBOutlet UITextField *titleField;
@property (strong, nonatomic) IBOutlet UITextField *descriptionField;
@property (strong, nonatomic) IBOutlet UILabel *startTimeField;
@property (strong, nonatomic) IBOutlet UILabel *endTimeField;
@property (strong, nonatomic) IBOutlet UISwitch *allDayField;
@property (strong, nonatomic) IBOutlet UILabel *daysField;
@property (strong, nonatomic) NSArray *days;
@property (strong, nonatomic) IBOutlet UILabel *appliesToField;
@property (strong, nonatomic) IBOutlet UILabel *notifyWhenField;
@property (strong, nonatomic) IBOutlet UILabel *changeExistsForField;
@property (strong, nonatomic) IBOutlet UILabel *contactsField;
@property (strong, nonatomic) NSArray *contactUUIDs;
@property (strong, nonatomic) IBOutlet UILabel *messageField;
@property (strong, nonatomic) IBOutlet UILabel *startDateField;
@property (strong, nonatomic) NSDate *startDate;
@property (strong, nonatomic) IBOutlet UILabel *endDateField;
@property (strong, nonatomic) NSDate *endDate;
- (IBAction)cancel:(id)sender;
- (IBAction)done:(id)sender;
@end
UtilizationDetailsVC.m相关方法:
- (void)selectIOVCDidCancel:(SelectIOVC *)controller
{
NSLog(@"%s",__PRETTY_FUNCTION__);
[self.navigationController popViewControllerAnimated:YES];
}
- (void)selectIOVCDidSave:(SelectIOVC *)controller
{
NSLog(@"%s",__PRETTY_FUNCTION__);
// Update input/output information with user selection on UI.
self.appliesToField.text = controller.ioDescription;
self.notifyWhenField.text = @"";
[self.navigationController popViewControllerAnimated:YES];
}
警报:
AlertDetailsVC.h:
#import <UIKit/UIKit.h>
#import "NCAlert.h"
#import "SelectTimeVC.h"
#import "SelectDaysVC.h"
#import "SelectIOVC.h"
#import "SelectIODetailsVC.h"
#import "SelectTimeLengthVC.h"
#import "MessageVC.h"
#import "AlertContactsVC.h"
@class AlertDetailsVC;
@protocol AlertDetailsVCDelegate <NSObject>
- (void)alertDetailsVCDidCancel: (AlertDetailsVC *)controller;
- (void)alertDetailsVCDidDelete: (AlertDetailsVC *)controller;
- (void)alertDetailsVCDidSave: (AlertDetailsVC *)controller;
@end
@interface AlertDetailsVC : UITableViewController <UITextFieldDelegate, UIActionSheetDelegate, SelectTimeVCDelegate, SelectDaysVCDelegate, SelectIOVCDelegate, SelectIODetailsVCDelegate, SelectTimeLengthVCDelegate, MessageVCDelegate, AlertContactsVCDelegate>
@property (weak, nonatomic) IBOutlet UIBarButtonItem *doneButton;
@property (strong, nonatomic) NCAlert *alert;
@property (weak, nonatomic) id <AlertDetailsVCDelegate> delegate;
@property (strong, nonatomic) NSString *identifier;
@property (strong, nonatomic) NSIndexPath *indexPath;
@property (strong, nonatomic) IBOutlet UITableViewCell *cellEnabled;
@property (strong, nonatomic) IBOutlet UITextField *titleField;
@property (strong, nonatomic) IBOutlet UITextField *descriptionField;
@property (strong, nonatomic) IBOutlet UILabel *startTimeField;
@property (strong, nonatomic) IBOutlet UILabel *endTimeField;
@property (strong, nonatomic) IBOutlet UISwitch *allDayField;
@property (strong, nonatomic) IBOutlet UILabel *daysField;
@property (strong, nonatomic) NSArray *days;
@property (strong, nonatomic) IBOutlet UILabel *appliesToField;
@property (strong, nonatomic) IBOutlet UILabel *notifyWhenField;
@property (strong, nonatomic) IBOutlet UILabel *changeExistsForField;
@property (strong, nonatomic) IBOutlet UISwitch *localAlertField;
@property (strong, nonatomic) IBOutlet UISwitch *localAlertAudioField;
@property (strong, nonatomic) IBOutlet UILabel *contactsField;
@property (strong, nonatomic) NSArray *contactUUIDs;
@property (strong, nonatomic) IBOutlet UILabel *messageField;
- (IBAction)cancel:(id)sender;
- (IBAction)done:(id)sender;
@end
AlertDetailsVC.m 相关方法:
- (void)selectIOVCDidCancel:(SelectIOVC *)controller
{
NSLog(@"%s",__PRETTY_FUNCTION__);
[self.navigationController popViewControllerAnimated:YES];
}
- (void)selectIOVCDidSave:(SelectIOVC *)controller
{
NSLog(@"%s",__PRETTY_FUNCTION__);
// Update input/output information with user selection on UI.
self.appliesToField.text = controller.ioDescription;
self.notifyWhenField.text = @"";
[self.navigationController popViewControllerAnimated:YES];
}
选择IOVC:
选择IOVC.h
#import <UIKit/UIKit.h>
@class SelectIOVC;
@protocol SelectIOVCDelegate <NSObject>
- (void)selectIOVCDidCancel: (SelectIOVC *)controller;
- (void)selectIOVCDidSave: (SelectIOVC *)controller;
@end
@interface SelectIOVC : UITableViewController <UIPickerViewDelegate, UIPickerViewDataSource> {
NSArray *selectIOType;
NSArray *selectIO;
}
@property (weak, nonatomic) IBOutlet UIBarButtonItem *doneButton;
@property (weak, nonatomic) id <SelectIOVCDelegate> delegate;
@property (weak, nonatomic) NSString *identifier;
@property (strong, nonatomic) IBOutlet UIPickerView *picker;
@property (assign, nonatomic) NSUInteger type;
@property (assign, nonatomic) NSUInteger io;
@property (assign, nonatomic) NSUInteger number;
@property (strong, nonatomic) NSString *ioDescription;
- (IBAction)cancel:(id)sender;
- (IBAction)done:(id)sender;
@end
SelectIOVC.m相关方法:
- (IBAction)cancel:(id)sender
{
[self.delegate selectIOVCDidCancel:self];
NSLog(@"%s",__PRETTY_FUNCTION__);
}
- (IBAction)done:(id)sender
{
NSLog(@"%s",__PRETTY_FUNCTION__);
NSString *typeDescription = [selectIOType objectAtIndex:self.type];
NSString *ioDescription = [selectIO objectAtIndex:self.io];
self.ioDescription = [NSString stringWithFormat:@"%@ %@ %01d", typeDescription, ioDescription, self.number];
[self.delegate selectIOVCDidSave:self];
}
- (void)viewDidUnload {
[self setDoneButton:nil];
[super viewDidUnload];
}
【问题讨论】:
-
很多时候,delegate什么都不做,是因为我忘记了delegate的设置。例如,您可能有一些看起来像
UtilizationDetailVC udvc = [UtilizationDetailVC alloc] init];的行,然后您可能会忘记这行udvc.delegate = self;。 -
我实际上
Synthesize是各自 .m 文件中UtilizationDetailVC的代表。然后我按照你所说的设置所有其他代表(例如SelectIOVC)。 -
@DinoTw 奇怪的是;我的
SelectIOVC的Done方法被调用。但是,我的UtilizationDetailVC中的SelectIOVCDidSave永远不会被调用。 -
@DinoTw 在这种情况下;就我的
AlertDetailsVC而言,两者都被正确调用。 -
解决了;参考我的帖子。
标签: objective-c methods uinavigationcontroller delegates uistoryboardsegue