我正在使用包含UITableViewController的PopoverView控制器的iPad应用程序,该应用程序具有主视图(在本例中为MKMapView)。 UITableview中的内容包含许多可变元素,我需要在MapView上放置注释引脚。这些变量取决于选择哪一行。我的想法是使用委托来实现这一点,但我正在努力实现它。使用委托在UIPopoverViewController和MainViewController之间进行通信
我已经在showIncidentList方法中声明了委托(请参见附件代码)。我的问题是,这个自定义委托是否适合这个期望的结果,如果是这样,我错过了什么,将允许我在委托中传递信息,并使用委托的更新信息调用主视图上的plotCallLocations方法。
培训相关的代码:
SearchViewController.h //所述popoverViewController类
@protocol IncidentPickerDelegate <NSObject>
- (void)incidentSelected:(NSMutableArray *)incidentDetail;
@end
@interface SearchViewController : UITableViewController <UITableViewDelegate, UITableViewDataSource> {
__weak id<IncidentPickerDelegate> _delegate;
}
@property (nonatomic, retain) NSMutableArray *incidentDetails;
@property (nonatomic, weak) id<IncidentPickerDelegate> delegate;
@end
SearchViewController.m
#import "TwitterSearchViewController.h"
#import "CallViewController.h"
@implementation SearchViewController
@synthesize delegate = _delegate;
@synthesize incidentDetails= _incidentDetails;
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
_incidentDetails = [[NSMutableArray alloc] initWithObjects:[textItems objectAtIndex:0], [textItems objectAtIndex:1], lat, lon, nil]; //textItems is an NSArray of parsed JSON data, lat and lon are int's
NSLog(@"_delegate = %@", _delegate);
if ([_delegate respondsToSelector:@selector(incidentSelected:)]) {
NSMutableArray *incidentDet = _incidentDetails;
[_delegate incidentSelected:incidentDet];
NSLog(@"incidentDetail ----> %@", incidentDet);
}
@end
CallViewController.h // MainViewController
#import "SearchViewController.h"
@interface CallViewController : UIViewController <CLLocationManagerDelegate, MKMapViewDelegate, UIAlertViewDelegate, IncidentPickerDelegate> {
}
@property (nonatomic, retain) UIPopoverController *incidnetListPopover;
@property (nonatomic, retain) SearchViewController *incidentPicker;
-(IBAction)showIncidentList:(id)sender;
CallViewController.m
#import "CallViewController.h"
#import "SearchViewController.h"
@implementation CallViewController
@synthesize incidnetListPopover = _incidnetListPopover;
@synthesize incidentPicker = _incidentPicker;
UIStoryboard* sb = [UIStoryboard storyboardWithName:@"MainStoryboard"
bundle:nil];
if (_incidentPicker == nil) {
self.incidentPicker = [sb instantiateViewControllerWithIdentifier:@"SearchViewController"];
_incidentPicker.delegate = self;
self.incidnetListPopover = [[UIPopoverController alloc]
initWithContentViewController:_incidentPicker];
}
[self.incidnetListPopover presentPopoverFromBarButtonItem:sender
permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
- (void)incidentSelected:(NSMutableArray *)incidentDetail {
// for (id<MKAnnotation> annotation in _mapView.annotations) {
// [_mapView removeAnnotation:annotation];
NSLog(@"plotCall called");
NSNumber * latitude = [incidentDetail objectAtIndex:2];
NSNumber * longitude = [incidentDetail objectAtIndex:3];
NSString * name = [incidentDetail objectAtIndex:1];
NSString * address = [incidentDetail objectAtIndex:0];
CLLocationCoordinate2D coordinate;
coordinate.latitude = latitude.doubleValue;
coordinate.longitude = longitude.doubleValue;
CallLocation *annotation = [[CallLocation alloc] initWithName:name address:address coordinate:coordinate];
[_mapView addAnnotation:annotation];
[self.incidnetListPopover dismissPopoverAnimated:YES];
}
奇怪的是,方法名称和委托名称是不同的我没有得到任何编译器警告或错误。我更新了我的代码和上面的内容以反映您的建议更改。到目前为止,没有运气让事件选择的方法运行。注意我有一个NSLog来测试代码是否被访问过,而不是。谢谢你的帮助! – blueHula
在didSelectRowAtIndexPath中放置断点或NSLog,并确保代码按预期运行。检查它是否通过了“respondsToSelector”条件。 – Anna
它似乎。我记录了刚刚传递了respondsToSelector条件的输出,并且在控制台中记录了NSMutableArray内容。 – blueHula